在Haskell中计算文件的MD5摘要 [英] Compute MD5 digest of file in Haskell

查看:77
本文介绍了在Haskell中计算文件的MD5摘要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Haskell,如何在不使用md5sum之类的外部工具的情况下计算文件的MD5摘要?

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

注意:,当我立即回答时,这个问题刻意显示没有任何努力.

Note: This question intentionally shows no effort as I answered it right away.

推荐答案

一种选择是使用 包,例如,如果您要计算文件foo.txt的哈希值:

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

此代码打印与md5sum foo.txt相同的MD5总和.

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

这篇关于在Haskell中计算文件的MD5摘要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆