diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-28 06:54:51 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-28 06:54:51 +0400 |
commit | 53d47a403f8e940f6f55f292c3d5d4b2edc0b3cb (patch) | |
tree | 2675d9309f1412510dccbc8bb64e1b4fe4e48920 | |
parent | 32027c2d4392fb6964bafe3aaf6d427e5d827d21 (diff) |
Hide InfoHash internal functions
-rw-r--r-- | src/Data/Torrent.hs | 12 | ||||
-rw-r--r-- | src/Data/Torrent/InfoHash.hs | 13 | ||||
-rw-r--r-- | tests/Data/Torrent/InfoHashSpec.hs | 6 |
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 | ||
67 | import Prelude hiding (sum) | 67 | import Prelude hiding (sum) |
68 | import Control.Applicative | 68 | import Control.Applicative |
69 | import qualified Crypto.Hash.SHA1 as C | ||
69 | import Control.DeepSeq | 70 | import Control.DeepSeq |
70 | import Control.Exception | 71 | import Control.Exception |
71 | import Control.Lens | 72 | import Control.Lens |
@@ -79,6 +80,7 @@ import qualified Data.ByteString.Lazy as BL | |||
79 | import Data.Char as Char | 80 | import Data.Char as Char |
80 | import Data.Hashable as Hashable | 81 | import Data.Hashable as Hashable |
81 | import qualified Data.List as L | 82 | import qualified Data.List as L |
83 | import Data.Maybe | ||
82 | import Data.Text as T | 84 | import Data.Text as T |
83 | import Data.Time | 85 | import Data.Time |
84 | import Data.Time.Clock.POSIX | 86 | import Data.Time.Clock.POSIX |
@@ -141,7 +143,7 @@ instance Hashable InfoDict where | |||
141 | infoDictionary :: LayoutInfo -> PieceInfo -> Bool -> InfoDict | 143 | infoDictionary :: LayoutInfo -> PieceInfo -> Bool -> InfoDict |
142 | infoDictionary li pinfo private = InfoDict ih li pinfo private | 144 | infoDictionary 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 | ||
147 | getPrivate :: Get Bool | 149 | getPrivate :: Get Bool |
@@ -151,6 +153,12 @@ putPrivate :: Bool -> BDict -> BDict | |||
151 | putPrivate False = id | 153 | putPrivate False = id |
152 | putPrivate True = \ cont -> "private" .=! True .: cont | 154 | putPrivate True = \ cont -> "private" .=! True .: cont |
153 | 155 | ||
156 | -- | Hash lazy bytestring using SHA1 algorithm. | ||
157 | hashLazyIH :: BL.ByteString -> InfoHash | ||
158 | hashLazyIH = fromMaybe (error msg) . byteStringToInfoHash . C.hashlazy | ||
159 | where | ||
160 | msg = "Infohash.hash: impossible: SHA1 is always 20 bytes long" | ||
161 | |||
154 | instance BEncode InfoDict where | 162 | instance 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 | ||
168 | ppPrivacy :: Bool -> Doc | 176 | ppPrivacy :: Bool -> Doc |
169 | ppPrivacy privacy = "Privacy: " <> if privacy then "private" else "public" | 177 | ppPrivacy 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 | ||
30 | import Control.Applicative | 26 | import Control.Applicative |
31 | import Control.Monad | 27 | import Control.Monad |
32 | import qualified Crypto.Hash.SHA1 as C | ||
33 | import Data.Aeson | 28 | import Data.Aeson |
34 | import Data.BEncode | 29 | import Data.BEncode |
35 | import Data.ByteString as BS | 30 | import Data.ByteString as BS |
@@ -151,14 +146,6 @@ shortHex = T.take 7 . longHex | |||
151 | ppHex :: BS.ByteString -> BS.ByteString | 146 | ppHex :: BS.ByteString -> BS.ByteString |
152 | ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed | 147 | ppHex = BL.toStrict . B.toLazyByteString . B.byteStringHexFixed |
153 | 148 | ||
154 | -- | Hash strict bytestring using SHA1 algorithm. | ||
155 | hash :: BS.ByteString -> InfoHash | ||
156 | hash = InfoHash . C.hash | ||
157 | |||
158 | -- | Hash lazy bytestring using SHA1 algorithm. | ||
159 | hashlazy :: BL.ByteString -> InfoHash | ||
160 | hashlazy = 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 @@ | |||
2 | module Data.Torrent.InfoHashSpec (spec) where | 2 | module Data.Torrent.InfoHashSpec (spec) where |
3 | 3 | ||
4 | import Control.Applicative | 4 | import Control.Applicative |
5 | import Data.ByteString as BS | ||
6 | import Data.Maybe | ||
5 | import System.FilePath | 7 | import System.FilePath |
6 | import Test.Hspec | 8 | import Test.Hspec |
7 | import Test.QuickCheck | 9 | import Test.QuickCheck |
@@ -12,7 +14,9 @@ import Data.Torrent.InfoHash as IH | |||
12 | 14 | ||
13 | 15 | ||
14 | instance Arbitrary InfoHash where | 16 | instance 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 | ||
17 | type TestPair = (FilePath, String) | 21 | type TestPair = (FilePath, String) |
18 | 22 | ||