diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-08 02:59:45 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-08 02:59:45 +0400 |
commit | d6c53fd0af4c395459f89a4e3fa9c5988c27ec32 (patch) | |
tree | b291b42f9248d3e96cced7185eabf2e1be6dcd1a /src/Data | |
parent | 4296031c4c1b004ec2271c0028ab603c3f6c4e8a (diff) |
+ mk newtype for info hash
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/Torrent.hs | 9 | ||||
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 41 |
2 files changed, 46 insertions, 4 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 128c1c7c..57831acf 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs | |||
@@ -2,7 +2,8 @@ | |||
2 | {-# LANGUAGE OverloadedStrings #-} | 2 | {-# LANGUAGE OverloadedStrings #-} |
3 | -- | This module provides torrent metainfo serialization. | 3 | -- | This module provides torrent metainfo serialization. |
4 | module Data.Torrent | 4 | module Data.Torrent |
5 | ( Torrent(..), TorrentInfo(..), TorrentFile(..) | 5 | ( module Data.Torrent.InfoHash |
6 | , Torrent(..), TorrentInfo(..), TorrentFile(..) | ||
6 | , fromFile | 7 | , fromFile |
7 | ) where | 8 | ) where |
8 | 9 | ||
@@ -13,8 +14,8 @@ import Data.ByteString (ByteString) | |||
13 | import qualified Data.ByteString as B | 14 | import qualified Data.ByteString as B |
14 | import qualified Data.ByteString.Char8 as BC (pack, unpack) | 15 | import qualified Data.ByteString.Char8 as BC (pack, unpack) |
15 | import Data.Text (Text) | 16 | import Data.Text (Text) |
16 | import Crypto.Hash.SHA1 | ||
17 | import Data.BEncode | 17 | import Data.BEncode |
18 | import Data.Torrent.InfoHash | ||
18 | import Network.URI | 19 | import Network.URI |
19 | 20 | ||
20 | type Time = Text | 21 | type Time = Text |
@@ -22,7 +23,7 @@ type Time = Text | |||
22 | -- TODO comment fields | 23 | -- TODO comment fields |
23 | -- TODO more convenient form of torrent info. | 24 | -- TODO more convenient form of torrent info. |
24 | data Torrent = Torrent { | 25 | data Torrent = Torrent { |
25 | tInfoHash :: ByteString | 26 | tInfoHash :: InfoHash |
26 | , tAnnounce :: URI | 27 | , tAnnounce :: URI |
27 | , tAnnounceList :: Maybe [[URI]] | 28 | , tAnnounceList :: Maybe [[URI]] |
28 | , tComment :: Maybe Text | 29 | , tComment :: Maybe Text |
@@ -148,4 +149,4 @@ instance BEncodable TorrentFile where | |||
148 | 149 | ||
149 | 150 | ||
150 | fromFile :: FilePath -> IO (Result Torrent) | 151 | fromFile :: FilePath -> IO (Result Torrent) |
151 | fromFile path = (fromBEncode <=< decode) <$> B.readFile path | 152 | fromFile filepath = (fromBEncode <=< decode) <$> B.readFile filepath |
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 @@ | |||
1 | module Data.Torrent.InfoHash | ||
2 | ( InfoHash (getInfoHash) | ||
3 | |||
4 | -- ^ Construction | ||
5 | , hash, hashlazy | ||
6 | |||
7 | -- ^ Extra | ||
8 | , ppHex | ||
9 | ) where | ||
10 | |||
11 | import Control.Applicative | ||
12 | import Data.Foldable | ||
13 | import Data.ByteString (ByteString) | ||
14 | import qualified Data.ByteString as B | ||
15 | import qualified Data.ByteString.Char8 as BC | ||
16 | import qualified Data.ByteString.Builder as B | ||
17 | import qualified Data.ByteString.Builder.Prim as B | ||
18 | import qualified Data.ByteString.Lazy as Lazy | ||
19 | import Data.Serialize | ||
20 | import qualified Crypto.Hash.SHA1 as C | ||
21 | |||
22 | -- | Exactly 20 bytes long SHA1 hash. | ||
23 | newtype InfoHash = InfoHash { getInfoHash :: ByteString } | ||
24 | deriving (Eq, Ord) | ||
25 | |||
26 | instance Show InfoHash where | ||
27 | show = BC.unpack . ppHex | ||
28 | |||
29 | instance Serialize InfoHash where | ||
30 | put = putByteString . getInfoHash | ||
31 | get = InfoHash <$> getBytes 20 | ||
32 | |||
33 | hash :: ByteString -> InfoHash | ||
34 | hash = InfoHash . C.hash | ||
35 | |||
36 | hashlazy :: Lazy.ByteString -> InfoHash | ||
37 | hashlazy = InfoHash . C.hashlazy | ||
38 | |||
39 | ppHex :: InfoHash -> ByteString | ||
40 | ppHex = Lazy.toStrict . B.toLazyByteString . | ||
41 | foldMap (B.primFixed B.word8HexFixed) . B.unpack . getInfoHash | ||