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.hs32
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 @@
12module Data.Torrent.InfoHash 12module 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
38import Data.URLEncoded as URL 46import Data.URLEncoded as URL
39import Data.Serialize 47import Data.Serialize
40import Data.String 48import Data.String
41import Data.Text 49import Data.Text as T
42import Data.Text.Encoding as T 50import Data.Text.Encoding as T
43import Network.URI 51import Network.URI
44import Numeric 52import 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.
115hash :: BS.ByteString -> InfoHash 123longHex :: InfoHash -> Text
116hash = InfoHash . C.hash 124longHex = T.decodeUtf8 . Base16.encode . getInfoHash
117 125
118-- | Hash lazy bytestring using SHA1 algorithm. 126-- | The same as 'longHex', but 7 character long.
119hashlazy :: BL.ByteString -> InfoHash 127shortHex :: InfoHash -> Text
120hashlazy = InfoHash . C.hashlazy 128shortHex = T.take 7 . longHex
121 129
122-- | Pretty print info hash in hexadecimal format. 130-- | Pretty print info hash in hexadecimal format.
123ppInfoHash :: InfoHash -> Doc 131ppInfoHash :: InfoHash -> Doc
@@ -126,6 +134,14 @@ ppInfoHash = text . BC.unpack . ppHex . getInfoHash
126ppHex :: BS.ByteString -> BS.ByteString 134ppHex :: BS.ByteString -> BS.ByteString
127ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed 135ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed
128 136
137-- | Hash strict bytestring using SHA1 algorithm.
138hash :: BS.ByteString -> InfoHash
139hash = InfoHash . C.hash
140
141-- | Hash lazy bytestring using SHA1 algorithm.
142hashlazy :: BL.ByteString -> InfoHash
143hashlazy = 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>