Example Links: http://efreedom.com/Question/1-1345851/MD5-File-Processing ; http://stackoverflow.com/questions/1177607/what-is-the-fastest-way-to-create-a-checksum-for-large-files-in-c/1177744#1177744
My intention for this post was to show how much more efficient it would be to use BufferedStream, however my results don’t show that. I’m guessing that somehow, the efficiency is happening under the covers in a place I don’t see.
If anyone knows this space well, please feel free to comment and suggest a better method. I’m publishing my source below for the test and my surprisingly similar results whether I used buffering or not.
Looking forward to the responses.
Console.WriteLine(str1 + " " + stopwatch1.ElapsedMilliseconds);
Console.WriteLine(str2 + " " + stopwatch2.ElapsedMilliseconds);
Console.ReadLine();
}
private static string GetChecksum(string file)
{
using (FileStream stream = File.OpenRead(file))
{
var sha = new SHA256Managed();
byte[] checksum = sha.ComputeHash(stream);
return BitConverter.ToString(checksum).Replace("-", String.Empty);
}
}
private static string GetChecksumBuffered(Stream stream)
{
using (var bufferedStream = new BufferedStream(stream, 1024 * 32))
{
var sha = new SHA256Managed();
byte[] checksum = sha.ComputeHash(bufferedStream);
return BitConverter.ToString(checksum).Replace("-", String.Empty);
}
}
}
Read more: PeterKellner.net