]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Utils.hs
refactoring: move verifyHash to Utils module
[functorrent.git] / src / FuncTorrent / Utils.hs
index ac08927dc348b905d326e58af99fe147b95930aa..48ebe67710d81258b9600c874dec3de414d52cf5 100644 (file)
@@ -3,16 +3,18 @@ module FuncTorrent.Utils
         writeFileAtOffset,
         readFileAtOffset,
         splitNum,
-        splitN
+        splitN,
+        verifyHash
        )
        where
 
-import Prelude hiding (writeFile)
+import Prelude hiding (writeFile, take)
 
+import qualified Crypto.Hash.SHA1 as SHA1 (hash)
+import Data.ByteString (ByteString, writeFile, hPut, hGet, take)
+import qualified Data.ByteString.Char8 as BC
 import System.IO (withFile, hSeek, IOMode(..), SeekMode(..))
 import System.Directory (doesFileExist)
-import Data.ByteString (ByteString, writeFile, hPut, hGet)
-import qualified Data.ByteString.Char8 as BC
 
 splitN :: Int -> BC.ByteString -> [BC.ByteString]
 splitN n bs | BC.null bs = []
@@ -42,3 +44,7 @@ readFileAtOffset path offset len =
   withFile path ReadWriteMode (\h -> do
                                   hSeek h AbsoluteSeek offset
                                   hGet h (fromInteger len))
+
+verifyHash :: ByteString -> ByteString -> Bool
+verifyHash bs pieceHash =
+  take 20 (SHA1.hash bs) == pieceHash