{- This file is part of monad-hash. - - Written in 2016 by fr33domlover . - - ♡ Copying is an act of love. Please copy, reuse and share. - - The author(s) have dedicated all copyright and related and neighboring - rights to this software to the public domain worldwide. This software is - distributed without any warranty. - - You should have received a copy of the CC0 Public Domain Dedication along - with this software. If not, see - . -} import Control.Monad (unless) import Control.Monad.IO.Class (liftIO) import Control.Monad.Trans.Hash import Crypto.Hash.Algorithms (SHA1) import System.Exit (exitFailure) import System.IO import qualified Data.ByteString as B incrementalHash :: Handle -> Int -> HashT SHA1 IO () incrementalHash h chunkSize = go where go = do b <- liftIO $ B.hGet h chunkSize unless (B.null b) $ do updateHash b go main :: IO () main = do let file = "COPYING" sha1sum = "82da472f6d00dc5f0a651f33ebb320aa9c7b08d0" chunkSize = 1000 (_, digest) <- withFile file ReadMode $ \ h -> runHashT $ incrementalHash h chunkSize let result = show digest putStrLn $ "Expecting: " ++ sha1sum putStrLn $ "Got: " ++ result if sha1sum == result then putStrLn "Success!" else putStrLn "Failed!" >> exitFailure