问题
I want to make it very clear and simple. What if I have 1gb ram and I'm trying to calculate md5 hash of 2gb file? Currently, I'm doing it this way:
private static string Md5Hash(byte[] input)
{
byte[] hash = MD5.Create().ComputeHash(input);
StringBuilder builder = new StringBuilder(32);
foreach(byte b in hash)
{ builder.Append(b.ToString("X2")); }
return builder.ToString();
}
// I'm using it like: 'Md5.AsString(File.ReadAllBytes(filePath))'
So what are your suggestions?
回答1:
Rather than computing the hash of the file after you've completely loaded it into memory, use the overload that takes a Stream
.
byte[] hash;
using (Stream input = File.OpenRead("Filename"))
{
hash = MD5.Create().ComputeHash(input);
}
来源:https://stackoverflow.com/questions/32707596/calculating-md5-hash-of-big-files