From 8d194406c7d23e610bf5227f52ee8e04a555e85d Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Sun, 5 Jan 2014 21:37:48 +0400 Subject: Add hashPiece function --- src/Data/Torrent/Piece.hs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src') 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 -- * Piece data , Piece (..) , pieceSize + , hashPiece -- * Piece control , HashList (..) @@ -121,6 +122,12 @@ defaultPieceSize x = max minPieceSize $ min maxPieceSize $ toPow2 pc -- Piece data -----------------------------------------------------------------------} +type PieceHash = ByteString + +hashsize :: Int +hashsize = 20 +{-# INLINE hashsize #-} + -- TODO check if pieceLength is power of 2 -- | Piece payload should be strict or lazy bytestring. data Piece a = Piece @@ -143,16 +150,14 @@ instance Pretty (Piece a) where pieceSize :: Piece BL.ByteString -> PieceSize pieceSize Piece {..} = fromIntegral (BL.length pieceData) +-- | Get piece hash. +hashPiece :: Piece BL.ByteString -> PieceHash +hashPiece Piece {..} = SHA1.hashlazy pieceData + {----------------------------------------------------------------------- -- Piece control -----------------------------------------------------------------------} -type PieceHash = ByteString - -hashsize :: Int -hashsize = 20 -{-# INLINE hashsize #-} - -- | A flat array of SHA1 hash for each piece. newtype HashList = HashList { unHashList :: ByteString } deriving (Show, Read, Eq, BEncode, Typeable) -- cgit v1.2.3