summaryrefslogtreecommitdiff
path: root/src/Data
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data')
-rw-r--r--src/Data/Torrent.hs12
-rw-r--r--src/Data/Torrent/InfoHash.hs13
2 files changed, 10 insertions, 15 deletions
diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs
index 8411dc97..e4b17c2b 100644
--- a/src/Data/Torrent.hs
+++ b/src/Data/Torrent.hs
@@ -66,6 +66,7 @@ module Data.Torrent
66 66
67import Prelude hiding (sum) 67import Prelude hiding (sum)
68import Control.Applicative 68import Control.Applicative
69import qualified Crypto.Hash.SHA1 as C
69import Control.DeepSeq 70import Control.DeepSeq
70import Control.Exception 71import Control.Exception
71import Control.Lens 72import Control.Lens
@@ -79,6 +80,7 @@ import qualified Data.ByteString.Lazy as BL
79import Data.Char as Char 80import Data.Char as Char
80import Data.Hashable as Hashable 81import Data.Hashable as Hashable
81import qualified Data.List as L 82import qualified Data.List as L
83import Data.Maybe
82import Data.Text as T 84import Data.Text as T
83import Data.Time 85import Data.Time
84import Data.Time.Clock.POSIX 86import Data.Time.Clock.POSIX
@@ -141,7 +143,7 @@ instance Hashable InfoDict where
141infoDictionary :: LayoutInfo -> PieceInfo -> Bool -> InfoDict 143infoDictionary :: LayoutInfo -> PieceInfo -> Bool -> InfoDict
142infoDictionary li pinfo private = InfoDict ih li pinfo private 144infoDictionary li pinfo private = InfoDict ih li pinfo private
143 where 145 where
144 ih = IH.hashlazy $ encode $ InfoDict fake_ih li pinfo private 146 ih = hashLazyIH $ encode $ InfoDict fake_ih li pinfo private
145 fake_ih = "0123456789012345678901234567890123456789" 147 fake_ih = "0123456789012345678901234567890123456789"
146 148
147getPrivate :: Get Bool 149getPrivate :: Get Bool
@@ -151,6 +153,12 @@ putPrivate :: Bool -> BDict -> BDict
151putPrivate False = id 153putPrivate False = id
152putPrivate True = \ cont -> "private" .=! True .: cont 154putPrivate True = \ cont -> "private" .=! True .: cont
153 155
156-- | Hash lazy bytestring using SHA1 algorithm.
157hashLazyIH :: BL.ByteString -> InfoHash
158hashLazyIH = fromMaybe (error msg) . byteStringToInfoHash . C.hashlazy
159 where
160 msg = "Infohash.hash: impossible: SHA1 is always 20 bytes long"
161
154instance BEncode InfoDict where 162instance BEncode InfoDict where
155 toBEncode InfoDict {..} = toDict $ 163 toBEncode InfoDict {..} = toDict $
156 putLayoutInfo idLayoutInfo $ 164 putLayoutInfo idLayoutInfo $
@@ -163,7 +171,7 @@ instance BEncode InfoDict where
163 <*> getPieceInfo 171 <*> getPieceInfo
164 <*> getPrivate 172 <*> getPrivate
165 where 173 where
166 ih = IH.hashlazy (encode dict) 174 ih = hashLazyIH (encode dict)
167 175
168ppPrivacy :: Bool -> Doc 176ppPrivacy :: Bool -> Doc
169ppPrivacy privacy = "Privacy: " <> if privacy then "private" else "public" 177ppPrivacy privacy = "Privacy: " <> if privacy then "private" else "public"
diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs
index fd57700a..e9e6cf39 100644
--- a/src/Data/Torrent/InfoHash.hs
+++ b/src/Data/Torrent/InfoHash.hs
@@ -21,15 +21,10 @@ module Data.Torrent.InfoHash
21 , shortHex 21 , shortHex
22 22
23 , addHashToURI 23 , addHashToURI
24
25 -- * Internal
26 , Data.Torrent.InfoHash.hash
27 , Data.Torrent.InfoHash.hashlazy
28 ) where 24 ) where
29 25
30import Control.Applicative 26import Control.Applicative
31import Control.Monad 27import Control.Monad
32import qualified Crypto.Hash.SHA1 as C
33import Data.Aeson 28import Data.Aeson
34import Data.BEncode 29import Data.BEncode
35import Data.ByteString as BS 30import Data.ByteString as BS
@@ -151,14 +146,6 @@ shortHex = T.take 7 . longHex
151ppHex :: BS.ByteString -> BS.ByteString 146ppHex :: BS.ByteString -> BS.ByteString
152ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed 147ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed
153 148
154-- | Hash strict bytestring using SHA1 algorithm.
155hash :: BS.ByteString -> InfoHash
156hash = InfoHash . C.hash
157
158-- | Hash lazy bytestring using SHA1 algorithm.
159hashlazy :: BL.ByteString -> InfoHash
160hashlazy = InfoHash . C.hashlazy
161
162-- | Add query info hash parameter to uri. 149-- | Add query info hash parameter to uri.
163-- 150--
164-- > info_hash=<url_encoded_info_hash> 151-- > info_hash=<url_encoded_info_hash>