George2
George2

Reputation: 45801

C# MD5 calculation issue

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?

Upvotes: 0

Views: 846

Answers (2)

dust2017
dust2017

Reputation: 1

This could work:

string hash=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(System.IO.File.ReadAllText(filename), "MD5")

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1503419

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.

Upvotes: 11

Related Questions