From 4d15ec8554520a1572e2fc168c53bd298a2ebbaa Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Sat, 25 Jul 2015 00:24:24 +0530
Subject: [PATCH] turn on hash verification of every downloaded piece

---
 src/FuncTorrent/Peer.hs | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/FuncTorrent/Peer.hs b/src/FuncTorrent/Peer.hs
index b193e43..0bce055 100644
--- a/src/FuncTorrent/Peer.hs
+++ b/src/FuncTorrent/Peer.hs
@@ -214,13 +214,14 @@ msgLoop pState pieceStatus | meInterested pState == False &&
                                Nothing -> putStrLn "Nothing to download"
                                Just workPiece -> do
                                  let pLen = len (pieceStatus ! workPiece)
+                                 putStrLn $ "piece length = " ++ show pLen
                                  pBS <- downloadPiece (handle pState) workPiece pLen
-                                 -- if not $ verifyHash pBS (hash (pieceStatus ! workPiece))
-                                 --  then
-                                 --  putStrLn $ "Hash mismatch: " ++ show (hash (pieceStatus ! workPiece)) ++ " vs " ++ show (take 20 (SHA1.hash pBS))
-                                 --  else do
-                                 writeFileAtOffset "/tmp/download.file" (workPiece * pLen) pBS
-                                 msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus)
+                                 if not $ verifyHash pBS (hash (pieceStatus ! workPiece))
+                                   then
+                                   putStrLn $ "Hash mismatch: " ++ show (hash (pieceStatus ! workPiece)) ++ " vs " ++ show (take 20 (SHA1.hash pBS))
+                                   else do
+                                   writeFileAtOffset "/tmp/download.file" (workPiece * pLen) pBS
+                                   msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus)
                           | otherwise = do
                               msg <- getMsg (handle pState)
                               putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show (peer pState)
-- 
2.45.2