計算文件的MD5校驗和

本文翻譯自:Calculate MD5 checksum for a file

I'm using iTextSharp to read the text from a PDF file. 我正在使用iTextSharp從PDF文件讀取文本。 However, there are times I cannot extract text, because the PDF file is only containing images. 但是,有時我無法提取文本,因爲PDF文件僅包含圖像。 I download the same PDF files everyday, and I want to see if the PDF has been modified. 我每天都下載相同的PDF文件,我想看看PDF是否已被修改。 If the text and modification date cannot be obtained, is a MD5 checksum the most reliable way to tell if the file has changed? 如果無法獲得文本和修改日期,則MD5校驗和是判斷文件是否已更改的最可靠方法嗎?

If it is, some code samples would be appreciated, because I don't have much experience with cryptography. 如果是這樣,將不勝感激一些代碼示例,因爲我在密碼學方面沒有太多經驗。


#1樓

參考:https://stackoom.com/question/i8kC/計算文件的MD-校驗和


#2樓

It's very simple using System.Security.Cryptography.MD5 : 使用System.Security.Cryptography.MD5非常簡單:

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

(I believe that actually the MD5 implementation used doesn't need to be disposed, but I'd probably still do so anyway.) (我相信實際上不需要處置使用的MD5實現,但是無論如何我還是會這樣做。)

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. 您可以將字節數組轉換爲例如base64,或直接比較字節。 (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.) (請注意,數組不會覆蓋Equals 。使用base64更容易解決問題,但如果您只對比較哈希值感興趣,則使用效率稍低。)

If you need to represent the hash as a string, you could convert it to hex using BitConverter : 如果您需要將散列表示爲字符串,則可以使用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();
        }
    }
}

#3樓

Here is a slightly simpler version that I found. 這是我發現的稍微簡單一些的版本。 It reads the entire file in one go and only requires a single using directive. 它可以一次性讀取整個文件,只需要一個using指令。

byte[] ComputeHash(string filePath)
{
    using (var md5 = MD5.Create())
    {
        return md5.ComputeHash(File.ReadAllBytes(filePath));
    }
}

#4樓

This is how I do it: 這是我的方法:

using System.IO;
using System.Security.Cryptography;

public string checkMD5(string filename)
{
    using (var md5 = MD5.Create())
    {
        using (var stream = File.OpenRead(filename))
        {
            return Encoding.Default.GetString(md5.ComputeHash(stream));
        }
    }
}

#5樓

I know this question was already answered, but this is what I use: 我知道已經回答了這個問題,但這是我使用的:

using (FileStream fStream = File.OpenRead(filename)) {
    return GetHash<MD5>(fStream)
}

Where GetHash : 哪裏GetHash

public static String GetHash<T>(Stream stream) where T : HashAlgorithm {
    StringBuilder sb = new StringBuilder();

    MethodInfo create = typeof(T).GetMethod("Create", new Type[] {});
    using (T crypt = (T) create.Invoke(null, null)) {
        byte[] hashBytes = crypt.ComputeHash(stream);
        foreach (byte bt in hashBytes) {
            sb.Append(bt.ToString("x2"));
        }
    }
    return sb.ToString();
}

Probably not the best way, but it can be handy. 可能不是最好的方法,但是它很方便。


#6樓

並且,如果您需要計算MD5以確定它是否與Azure blob的MD5相匹配,那麼此SO問答可能會有所幫助: 在Azure上上傳的blob的MD5哈希與本地計算機上的相同文件不匹配

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章