计算MD5校验和文件文件

2023-09-02 20:41:53 作者:王者要野心

我使用 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.)

 
精彩推荐
图片推荐