diff options
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index ac13aa6c..2e3946eb 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs | |||
@@ -12,10 +12,18 @@ | |||
12 | module Data.Torrent.InfoHash | 12 | module Data.Torrent.InfoHash |
13 | ( -- * Info hash | 13 | ( -- * Info hash |
14 | InfoHash(..) | 14 | InfoHash(..) |
15 | |||
16 | -- * Parsing | ||
15 | , textToInfoHash | 17 | , textToInfoHash |
16 | , addHashToURI | 18 | |
19 | -- * Rendering | ||
20 | , longHex | ||
21 | , shortHex | ||
17 | , ppInfoHash | 22 | , ppInfoHash |
18 | 23 | ||
24 | , addHashToURI | ||
25 | |||
26 | |||
19 | , Data.Torrent.InfoHash.hash | 27 | , Data.Torrent.InfoHash.hash |
20 | , Data.Torrent.InfoHash.hashlazy | 28 | , Data.Torrent.InfoHash.hashlazy |
21 | ) where | 29 | ) where |
@@ -38,7 +46,7 @@ import Data.Hashable as Hashable | |||
38 | import Data.URLEncoded as URL | 46 | import Data.URLEncoded as URL |
39 | import Data.Serialize | 47 | import Data.Serialize |
40 | import Data.String | 48 | import Data.String |
41 | import Data.Text | 49 | import Data.Text as T |
42 | import Data.Text.Encoding as T | 50 | import Data.Text.Encoding as T |
43 | import Network.URI | 51 | import Network.URI |
44 | import Numeric | 52 | import Numeric |
@@ -111,13 +119,13 @@ textToInfoHash text | |||
111 | hashLen = BS.length hashStr | 119 | hashLen = BS.length hashStr |
112 | hashStr = T.encodeUtf8 text | 120 | hashStr = T.encodeUtf8 text |
113 | 121 | ||
114 | -- | Hash strict bytestring using SHA1 algorithm. | 122 | -- | Hex encode infohash to text, full length. |
115 | hash :: BS.ByteString -> InfoHash | 123 | longHex :: InfoHash -> Text |
116 | hash = InfoHash . C.hash | 124 | longHex = T.decodeUtf8 . Base16.encode . getInfoHash |
117 | 125 | ||
118 | -- | Hash lazy bytestring using SHA1 algorithm. | 126 | -- | The same as 'longHex', but 7 character long. |
119 | hashlazy :: BL.ByteString -> InfoHash | 127 | shortHex :: InfoHash -> Text |
120 | hashlazy = InfoHash . C.hashlazy | 128 | shortHex = T.take 7 . longHex |
121 | 129 | ||
122 | -- | Pretty print info hash in hexadecimal format. | 130 | -- | Pretty print info hash in hexadecimal format. |
123 | ppInfoHash :: InfoHash -> Doc | 131 | ppInfoHash :: InfoHash -> Doc |
@@ -126,6 +134,14 @@ ppInfoHash = text . BC.unpack . ppHex . getInfoHash | |||
126 | ppHex :: BS.ByteString -> BS.ByteString | 134 | ppHex :: BS.ByteString -> BS.ByteString |
127 | ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed | 135 | ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed |
128 | 136 | ||
137 | -- | Hash strict bytestring using SHA1 algorithm. | ||
138 | hash :: BS.ByteString -> InfoHash | ||
139 | hash = InfoHash . C.hash | ||
140 | |||
141 | -- | Hash lazy bytestring using SHA1 algorithm. | ||
142 | hashlazy :: BL.ByteString -> InfoHash | ||
143 | hashlazy = InfoHash . C.hashlazy | ||
144 | |||
129 | -- | Add query info hash parameter to uri. | 145 | -- | Add query info hash parameter to uri. |
130 | -- | 146 | -- |
131 | -- > info_hash=<url_encoded_info_hash> | 147 | -- > info_hash=<url_encoded_info_hash> |