summaryrefslogtreecommitdiff
path: root/src/Data/Torrent
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-08 02:59:45 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-08 02:59:45 +0400
commitd6c53fd0af4c395459f89a4e3fa9c5988c27ec32 (patch)
treeb291b42f9248d3e96cced7185eabf2e1be6dcd1a /src/Data/Torrent
parent4296031c4c1b004ec2271c0028ab603c3f6c4e8a (diff)
+ mk newtype for info hash
Diffstat (limited to 'src/Data/Torrent')
-rw-r--r--src/Data/Torrent/InfoHash.hs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs
new file mode 100644
index 00000000..448e9a5a
--- /dev/null
+++ b/src/Data/Torrent/InfoHash.hs
@@ -0,0 +1,41 @@
1module Data.Torrent.InfoHash
2 ( InfoHash (getInfoHash)
3
4 -- ^ Construction
5 , hash, hashlazy
6
7 -- ^ Extra
8 , ppHex
9 ) where
10
11import Control.Applicative
12import Data.Foldable
13import Data.ByteString (ByteString)
14import qualified Data.ByteString as B
15import qualified Data.ByteString.Char8 as BC
16import qualified Data.ByteString.Builder as B
17import qualified Data.ByteString.Builder.Prim as B
18import qualified Data.ByteString.Lazy as Lazy
19import Data.Serialize
20import qualified Crypto.Hash.SHA1 as C
21
22-- | Exactly 20 bytes long SHA1 hash.
23newtype InfoHash = InfoHash { getInfoHash :: ByteString }
24 deriving (Eq, Ord)
25
26instance Show InfoHash where
27 show = BC.unpack . ppHex
28
29instance Serialize InfoHash where
30 put = putByteString . getInfoHash
31 get = InfoHash <$> getBytes 20
32
33hash :: ByteString -> InfoHash
34hash = InfoHash . C.hash
35
36hashlazy :: Lazy.ByteString -> InfoHash
37hashlazy = InfoHash . C.hashlazy
38
39ppHex :: InfoHash -> ByteString
40ppHex = Lazy.toStrict . B.toLazyByteString .
41 foldMap (B.primFixed B.word8HexFixed) . B.unpack . getInfoHash