From: Ramakrishnan Muthukrishnan Date: Fri, 24 Jul 2015 18:54:24 +0000 (+0530) Subject: turn on hash verification of every downloaded piece X-Git-Url: https://git.rkrishnan.org/specifications/%5B/%5D%20/%22news.html/index.php?a=commitdiff_plain;h=4d15ec8554520a1572e2fc168c53bd298a2ebbaa;p=functorrent.git turn on hash verification of every downloaded piece --- 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)