一、前言
public class Md5Helper
{
public static string Md5(string value)
{
var result = string.Empty;
if (string.IsNullOrEmpty(value)) return result;
using (var md5 = MD5.Create())
{
result = GetMd5Hash(md5, value);
}
return result;
}
static string GetMd5Hash(MD5 md5Hash, string input)
{
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
var sBuilder = new StringBuilder();
foreach (byte t in data)
{
sBuilder.Append(t.ToString("x2"));
}
return sBuilder.ToString();
}
static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash)
{
var hashOfInput = GetMd5Hash(md5Hash, input);
var comparer = StringComparer.OrdinalIgnoreCase;
return 0 == comparer.Compare(hashOfInput, hash);
}
}