计算文件MD5值的方法 MD5CryptoServiceProvider 散列算法
//计算文件MD5值的方法:
/// <summary>
/// 计算文件MD5值
/// </summary>
/// <param name="str">需要计算的文件路径</param>
/// <returns>MD5值</returns>
public static string MD5Value(String filepath)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] md5ch;
using (FileStream fs = File.OpenRead(filepath))
{
md5ch = md5.ComputeHash(fs);
}
md5.Clear();
string strMd5 = "";
for (int i = 0; i < md5ch.Length - 1; i++)
{
strMd5 += md5ch[i].ToString("x").PadLeft(2, '0');
}
return strMd5;
}
public string UseUTF8EncodeEncrypt(string plain)
{
MD5 md5 = MD5.Create();
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(plain));
string ra = "";
foreach (var item in result)
{
ra += item.ToString("x2");
}
return ra;
}