diff options
Diffstat (limited to 'src/Data/Torrent/InfoHash.hs')
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 20 |
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 | |||
54 | import Text.PrettyPrint.Class | 55 | import 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. |
58 | newtype InfoHash = InfoHash { getInfoHash :: BS.ByteString } | 69 | newtype InfoHash = InfoHash { getInfoHash :: BS.ByteString } |
59 | deriving (Eq, Ord) | 70 | deriving (Eq, Ord) |
@@ -110,6 +121,15 @@ instance URLShow InfoHash where | |||
110 | instance Pretty InfoHash where | 121 | instance Pretty InfoHash where |
111 | pretty = text . BC.unpack . ppHex . getInfoHash | 122 | pretty = text . BC.unpack . ppHex . getInfoHash |
112 | 123 | ||
124 | infoHashLen :: Int | ||
125 | infoHashLen = 20 | ||
126 | |||
127 | -- | Convert raw bytes to info hash. | ||
128 | byteStringToInfoHash :: BS.ByteString -> Maybe InfoHash | ||
129 | byteStringToInfoHash 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. |
114 | textToInfoHash :: Text -> Maybe InfoHash | 134 | textToInfoHash :: Text -> Maybe InfoHash |
115 | textToInfoHash t | 135 | textToInfoHash t |