Calculate MD5 file digest in Haskell

Using Haskell, how can I calculate the digest of an MD5 file without using external tools like md5sum ?

Note: This question intentionally does not show any effort since I answered it immediately.

+5
source share
2 answers

One option is to use the pureMD5 package, for example, if you want to calculate the hash of the foo.txt file:

 import qualified Data.ByteString.Lazy as LB import Data.Digest.Pure.MD5 main :: IO () main = do fileContent <- LB.readFile "foo.txt" let md5Digest = md5 fileContent print md5Digest 

This code prints the same MD5 sum as md5sum foo.txt .

If you prefer single-line, you can use it (the import is the same as above):

 LB.readFile "foo.txt" >>= print . md5 
+3
source

Another option would be to use cryptohash , which is based on a C implementation, and also provides other hashing algorithms such as 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 
+1
source

All Articles