summaryrefslogtreecommitdiff
path: root/src/Data/Torrent/InfoHash.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Torrent/InfoHash.hs')
-rw-r--r--src/Data/Torrent/InfoHash.hs20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs
index 3d2bbe25..a4d6a02e 100644
--- a/src/Data/Torrent/InfoHash.hs
+++ b/src/Data/Torrent/InfoHash.hs
@@ -14,6 +14,7 @@ module Data.Torrent.InfoHash
14 InfoHash(..) 14 InfoHash(..)
15 15
16 -- * Parsing 16 -- * Parsing
17 , byteStringToInfoHash
17 , textToInfoHash 18 , textToInfoHash
18 19
19 -- * Rendering 20 -- * Rendering
@@ -54,6 +55,16 @@ import Text.PrettyPrint
54import Text.PrettyPrint.Class 55import Text.PrettyPrint.Class
55 56
56 57
58-- TODO
59--
60-- data Word160 = Word160 {-# UNPACK #-} !Word64
61-- {-# UNPACK #-} !Word64
62-- {-# UNPACK #-} !Word32
63--
64-- newtype InfoHash = InfoHash Word160
65--
66-- reason: bytestring have overhead = 8 words, while infohash have length 20 bytes
67
57-- | Exactly 20 bytes long SHA1 hash of the info part of torrent file. 68-- | Exactly 20 bytes long SHA1 hash of the info part of torrent file.
58newtype InfoHash = InfoHash { getInfoHash :: BS.ByteString } 69newtype InfoHash = InfoHash { getInfoHash :: BS.ByteString }
59 deriving (Eq, Ord) 70 deriving (Eq, Ord)
@@ -110,6 +121,15 @@ instance URLShow InfoHash where
110instance Pretty InfoHash where 121instance Pretty InfoHash where
111 pretty = text . BC.unpack . ppHex . getInfoHash 122 pretty = text . BC.unpack . ppHex . getInfoHash
112 123
124infoHashLen :: Int
125infoHashLen = 20
126
127-- | Convert raw bytes to info hash.
128byteStringToInfoHash :: BS.ByteString -> Maybe InfoHash
129byteStringToInfoHash bs
130 | BS.length bs == infoHashLen = Just (InfoHash bs)
131 | otherwise = Nothing
132
113-- | Tries both base16 and base32 while decoding info hash. 133-- | Tries both base16 and base32 while decoding info hash.
114textToInfoHash :: Text -> Maybe InfoHash 134textToInfoHash :: Text -> Maybe InfoHash
115textToInfoHash t 135textToInfoHash t