Compute MD5 digest of file in Haskell

前端 未结 2 1083
不知归路
不知归路 2021-01-11 23:55

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

Note: This question intentionally s

2条回答
  •  生来不讨喜
    2021-01-12 00:22

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

    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 the same MD5 sum as md5sum foo.txt.

    If you prefer a one-liner, you can use this one (the imports are the same as above):

    LB.readFile "foo.txt" >>= print . md5
    

提交回复
热议问题