C# 获取MD5
时间:2011-03-02 来源:圈圈点点
using System.IO;
using System.Security.Cryptography;
//获取文件的MD5码
public static string CretaeMD5(string fileName)
{
string hashStr = string.Empty;
try
{
FileStream fs = new FileStream(
fileName,
FileMode.Open,
FileAccess.Read,
FileShare.Read);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(fs);
hashStr = ByteArrayToHexString(hash);
fs.Close();
fs.Dispose();
}
catch (Exception ex)
{
throw ex;
}
return hashStr;
}
//获取流的MD5码
public static string CretaeMD5(Stream stream)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(stream);
return ByteArrayToHexString(hash);
}
//获取byte数组中指定部分的MD5码
public static string CretaeMD5(byte[] buffer, int offset, int count)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(buffer, offset, count);
return ByteArrayToHexString(hash);
}
private static string ByteArrayToHexString(byte[] values)
{
StringBuilder sb = new StringBuilder();
foreach (byte value in values)
{
sb.AppendFormat("{0:X2}", value);
}
return sb.ToString();
}
private static string ByteArrayToHexString(byte[] values)
{
StringBuilder sb = new StringBuilder();
foreach (byte value in values)
{
sb.AppendFormat("{0:X2}", value);
}
return sb.ToString();
}