summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-28 06:54:51 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-28 06:54:51 +0400
commit53d47a403f8e940f6f55f292c3d5d4b2edc0b3cb (patch)
tree2675d9309f1412510dccbc8bb64e1b4fe4e48920
parent32027c2d4392fb6964bafe3aaf6d427e5d827d21 (diff)
Hide InfoHash internal functions
-rw-r--r--src/Data/Torrent.hs12
-rw-r--r--src/Data/Torrent/InfoHash.hs13
-rw-r--r--tests/Data/Torrent/InfoHashSpec.hs6
3 files changed, 15 insertions, 16 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>
diff --git a/tests/Data/Torrent/InfoHashSpec.hs b/tests/Data/Torrent/InfoHashSpec.hs
index ba9ce9a4..afa017ef 100644
--- a/tests/Data/Torrent/InfoHashSpec.hs
+++ b/tests/Data/Torrent/InfoHashSpec.hs
@@ -2,6 +2,8 @@
2module Data.Torrent.InfoHashSpec (spec) where 2module Data.Torrent.InfoHashSpec (spec) where
3 3
4import Control.Applicative 4import Control.Applicative
5import Data.ByteString as BS
6import Data.Maybe
5import System.FilePath 7import System.FilePath
6import Test.Hspec 8import Test.Hspec
7import Test.QuickCheck 9import Test.QuickCheck
@@ -12,7 +14,9 @@ import Data.Torrent.InfoHash as IH
12 14
13 15
14instance Arbitrary InfoHash where 16instance Arbitrary InfoHash where
15 arbitrary = IH.hash <$> arbitrary 17 arbitrary = do
18 bs <- BS.pack <$> vectorOf 20 arbitrary
19 pure $ fromMaybe (error "arbitrary infohash") $ byteStringToInfoHash bs
16 20
17type TestPair = (FilePath, String) 21type TestPair = (FilePath, String)
18 22