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

查看:128
本文介绍了计算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?

如果是这样,有些code样品将AP preciated,因为我没有与密码学了不​​少经验。

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

推荐答案

这是非常简单的:

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例如,或直接比较的字节数。 (要知道,阵列不覆盖等于。使用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.)

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

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