C# MD5 calculation issue

2020-08-01 06:52发布

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?

2条回答
三岁会撩人
2楼-- · 2020-08-01 07:28

This could work:

string hash=System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(System.IO.File.ReadAllText(filename), "MD5")
查看更多
欢心
3楼-- · 2020-08-01 07:45

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.

查看更多
登录 后发表回答