Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute MD5 digest of file in Haskell

Tags:

haskell

md5

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

like image 338
Uli Köhler Avatar asked May 29 '15 18:05

Uli Köhler


2 Answers

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
like image 52
Uli Köhler Avatar answered Sep 23 '22 07:09

Uli Köhler


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.)

like image 3
Atemu Avatar answered Sep 22 '22 07:09

Atemu