diff options
-rw-r--r-- | src/Data/Torrent/Piece.hs | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs index a7ea4700..74a2469a 100644 --- a/src/Data/Torrent/Piece.hs +++ b/src/Data/Torrent/Piece.hs | |||
@@ -24,6 +24,7 @@ module Data.Torrent.Piece | |||
24 | -- * Piece data | 24 | -- * Piece data |
25 | , Piece (..) | 25 | , Piece (..) |
26 | , pieceSize | 26 | , pieceSize |
27 | , hashPiece | ||
27 | 28 | ||
28 | -- * Piece control | 29 | -- * Piece control |
29 | , HashList (..) | 30 | , HashList (..) |
@@ -121,6 +122,12 @@ defaultPieceSize x = max minPieceSize $ min maxPieceSize $ toPow2 pc | |||
121 | -- Piece data | 122 | -- Piece data |
122 | -----------------------------------------------------------------------} | 123 | -----------------------------------------------------------------------} |
123 | 124 | ||
125 | type PieceHash = ByteString | ||
126 | |||
127 | hashsize :: Int | ||
128 | hashsize = 20 | ||
129 | {-# INLINE hashsize #-} | ||
130 | |||
124 | -- TODO check if pieceLength is power of 2 | 131 | -- TODO check if pieceLength is power of 2 |
125 | -- | Piece payload should be strict or lazy bytestring. | 132 | -- | Piece payload should be strict or lazy bytestring. |
126 | data Piece a = Piece | 133 | data Piece a = Piece |
@@ -143,16 +150,14 @@ instance Pretty (Piece a) where | |||
143 | pieceSize :: Piece BL.ByteString -> PieceSize | 150 | pieceSize :: Piece BL.ByteString -> PieceSize |
144 | pieceSize Piece {..} = fromIntegral (BL.length pieceData) | 151 | pieceSize Piece {..} = fromIntegral (BL.length pieceData) |
145 | 152 | ||
153 | -- | Get piece hash. | ||
154 | hashPiece :: Piece BL.ByteString -> PieceHash | ||
155 | hashPiece Piece {..} = SHA1.hashlazy pieceData | ||
156 | |||
146 | {----------------------------------------------------------------------- | 157 | {----------------------------------------------------------------------- |
147 | -- Piece control | 158 | -- Piece control |
148 | -----------------------------------------------------------------------} | 159 | -----------------------------------------------------------------------} |
149 | 160 | ||
150 | type PieceHash = ByteString | ||
151 | |||
152 | hashsize :: Int | ||
153 | hashsize = 20 | ||
154 | {-# INLINE hashsize #-} | ||
155 | |||
156 | -- | A flat array of SHA1 hash for each piece. | 161 | -- | A flat array of SHA1 hash for each piece. |
157 | newtype HashList = HashList { unHashList :: ByteString } | 162 | newtype HashList = HashList { unHashList :: ByteString } |
158 | deriving (Show, Read, Eq, BEncode, Typeable) | 163 | deriving (Show, Read, Eq, BEncode, Typeable) |