C#获取文件/字节数组MD5值方法
Posted 源头活水
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#获取文件/字节数组MD5值方法相关的知识,希望对你有一定的参考价值。
找了很多,就这个管用,有时间好好研究一番
public static string GetMD5Hash(string fileName)
{
try
{
FileStream file = new FileStream(fileName, FileMode.Open);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(file);
file.Close();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("GetMD5Hash() fail,error:" + ex.Message);
}
}
public static string GetMD5Hash(byte[] bytedata)
{
try
{
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] retVal = md5.ComputeHash(bytedata);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < retVal.Length; i++)
{
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
throw new Exception("GetMD5Hash() fail,error:" + ex.Message);
}
}
来源网址:
http://blog.csdn.net/snakorse/article/details/19578519
以上是关于C#获取文件/字节数组MD5值方法的主要内容,如果未能解决你的问题,请参考以下文章