Using Haskell, how can I compute the MD5 digest of a file without using external tools like md5sum
?
Another option would be using cryptohash
which is based on a C implementation and also provides other hashes algorithms like SHA1:
import qualified Data.ByteString.Lazy as LB
import Crypto.Hash
md5 :: LB.ByteString -> Digest MD5
md5 = hashlazy
main :: IO ()
main = do
fileContent <- LB.readFile "foo.txt"
let md5Digest = md5 fileContent
print $ digestToHexByteString md5Digest
You should be using cryptonite nowadays:
import System.Environment
import Crypto.Hash
import qualified Data.ByteString.Lazy as L
main = do
content <- L.readFile "foo.txt"
let digest = hashlazy content :: Digest MD5
putStrLn $ show digest
(You can actually replace MD5
with any hash algorithm cryptonite supports, SHA256
for instance.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With