C# 计算文件的 MD5 校验和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10520048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calculate MD5 checksum for a file
提问by broke
I'm using iTextSharpto 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 MD5checksum the most reliable way to tell if the file has changed?
我正在使用iTextSharp从 PDF 文件中读取文本。但是,有时我无法提取文本,因为 PDF 文件仅包含图像。我每天下载相同的PDF文件,我想看看PDF是否被修改过。如果无法获取文本和修改日期,MD5校验和是否是判断文件是否已更改的最可靠方法?
If it is, some code samples would be appreciated, because I don't have much experience with cryptography.
如果是,一些代码示例将不胜感激,因为我对密码学没有太多经验。
采纳答案by Jon Skeet
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 actuallythe 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. (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.)
事后如何比较结果取决于您;例如,您可以将字节数组转换为 base64,或者直接比较字节。(请注意,数组不会覆盖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();
}
}
}
回答by Ashley Davis
Here is a slightly simpler version that I found. It reads the entire file in one go and only requires a single usingdirective.
这是我找到的一个稍微简单的版本。它一次性读取整个文件,只需要一个using指令。
byte[] ComputeHash(string filePath)
{
using (var md5 = MD5.Create())
{
return md5.ComputeHash(File.ReadAllBytes(filePath));
}
}
回答by BoliBerrys
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));
}
}
}
回答by Badaro Jr.
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.
可能不是最好的方法,但它可以很方便。
回答by Manfred
And if you need to calculate the MD5 to see whether it matches the MD5 of an Azure blob, then this SO question and answer might be helpful: MD5 hash of blob uploaded on Azure doesnt match with same file on local machine
如果您需要计算 MD5 以查看它是否与 Azure blob 的 MD5 匹配,那么这个问题和答案可能会有所帮助:Azure 上上传的 blob 的 MD5 哈希与本地计算机上的相同文件不匹配
回答by Romil Kumar Jain
I know that I am late to party but performed test before actually implement the solution.
我知道我迟到了,但在实际实施解决方案之前进行了测试。
I did perform test against inbuilt MD5 class and also md5sum.exe. In my case inbuilt class took 13 second where md5sum.exe too around 16-18 seconds in every run.
我确实对内置的 MD5 类和md5sum.exe进行了测试。在我的例子中,内置类需要 13 秒,其中 md5sum.exe 在每次运行中也需要大约 16-18 秒。
DateTime current = DateTime.Now;
string file = @"C:\text.iso";//It's 2.5 Gb file
string output;
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(file))
{
byte[] checksum = md5.ComputeHash(stream);
output = BitConverter.ToString(checksum).Replace("-", String.Empty).ToLower();
Console.WriteLine("Total seconds : " + (DateTime.Now - current).TotalSeconds.ToString() + " " + output);
}
}

