/// <summary>
/// MD5验证函数
/// </summary>
/// <param name="fileName">文件的路径</param>
/// <param name="blockletter">返回的MD5值是否为大写</param>
/// <returns></returns>
public static string GetMD5HashFromFile(string fileName,bool blockletter)
{
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 = ; i < retVal.Length; i++)
{
if (blockletter)
sb.Append(retVal[i].ToString("X2"));
else
sb.Append(retVal[i].ToString("x2"));
}
return sb.ToString();
}
catch (Exception ex)
{
return "文件读取错误";
}
}
/// <summary>
/// 计算SHA1值
/// </summary>
/// <param name="fileName">文件地址</param>
/// <param name="blockletter">是否输出大写</param>
/// <returns></returns>
public static string GetSHA1HashFromFile(string fileName, bool blockletter)
{
var tragetFile = new System.IO.FileStream(fileName, System.IO.FileMode.Open);
var sha1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
byte[] hashbytes = sha1.ComputeHash(tragetFile);
tragetFile.Close();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = ; i < hashbytes.Length; i++)
{
if (blockletter) //判断输出大小写
sb.Append(hashbytes[i].ToString("X2"));
else
sb.Append(hashbytes[i].ToString("x2"));
}
return sb.ToString();
}