计算文件的 MD5 校验和 [英] Calculate MD5 checksum for a file

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

问题描述

我正在使用 iTextSharp 从 PDF 文件中读取文本.但是,有时我无法提取文本,因为 PDF 文件仅包含图像.我每天下载相同的PDF文件,我想看看PDF是否被修改过.如果无法获得文本和修改日期,MD5 校验和是判断是否正确的最可靠方法文件变了?

I'm using iTextSharp to read the text from a PDF file. However, there are times I cannot extract text, because the PDF file is only containing images. I download the same PDF files everyday, and I want to see if the PDF has been modified. If the text and modification date cannot be obtained, is a MD5 checksum the most reliable way to tell if the file has changed?

如果是,一些代码示例将不胜感激,因为我在密码学方面没有太多经验.

If it is, some code samples would be appreciated, because I don't have much experience with cryptography.

推荐答案

使用 System.Security.Cryptography.MD5:

using (var md5 = MD5.Create())
{
    using (var stream = File.OpenRead(filename))
    {
        return md5.ComputeHash(stream);
    }
}

(我相信实际上使用的 MD5 实现不需要被处理,但我可能仍然会这样做.)

(I believe that actually the MD5 implementation used doesn't need to be disposed, but I'd probably still do so anyway.)

之后如何比较结果取决于您;例如,您可以将字节数组转换为 base64,或直接比较字节.(请注意,数组不会覆盖 Equals.使用 base64 更容易获得正确的结果,但如果您真的只对比较哈希感兴趣,则效率会稍低一些.)

How you compare the results afterwards is up to you; you can convert the byte array to base64 for example, or compare the bytes directly. (Just be aware that arrays don't override Equals. Using base64 is simpler to get right, but slightly less efficient if you're really only interested in comparing the hashes.)

如果您需要将哈希表示为字符串,可以使用 BitConverter 将其转换为十六进制:

If you need to represent the hash as a string, you could convert it to hex using BitConverter:

static string CalculateMD5(string filename)
{
    using (var md5 = MD5.Create())
    {
        using (var stream = File.OpenRead(filename))
        {
            var hash = md5.ComputeHash(stream);
            return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
        }
    }
}

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

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