views:

227

answers:

1

Hello everyone,

I am using VSTS 2008 + C# + .Net 3.0. I want to find the most efficient way to calculate the MD5 result for the whole content of a txt file.

What is the most efficient solution?

thanks in advance, George

+9  A: 

Something as simple as:

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

Given that there's no way of avoiding reading every byte of the stream, I doubt that you'll find anything significantly more efficient.

Jon Skeet
Jon, could you please not reply so darn fast all the time :)
Brian Rasmussen
That is called Experience I guess ;)
Chathuranga Chandrasekara
I wonder - does ComputeHash() read the stream byte-by-byte or in blocks? In the first case there is some room for optimization.
Vilx-
@Vilx: While I haven't checked, I'd be amazed if it didn't do it in an efficient way.
Jon Skeet
@Brian: I gave you about 2 minutes. What more could you ask for? ;)
Jon Skeet
@Vilx: I think `ComputeHash` reads the stream in 4KB chunks, not byte-by-byte.
LukeH
Thanks, question answered!
George2