怎样使用C# MD5加密来增强密码的安全度

一、前言

二、MD5Helper

  • C#代码实现:
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);
    }
}
posted @ 2018-08-21 08:41  君吟  阅读(409)  评论(0编辑  收藏  举报