C#的MD5哈希值计算

MD5哈希值计算:(仅仅是记录一下)

/// <summary>
/// 获取字符串的MD5值
/// </summary>
/// <param name="str">字符串</param>
/// <param name="salt">加盐值</param>
/// <returns></returns>
public static string MD5(this string str, string salt = "")
{
    //MD5计算类
    using (System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
    {
        byte[] bytValue, bytHash;
        //将要计算的字符串转换为字节数组
        bytValue = System.Text.Encoding.UTF8.GetBytes(salt + str);
        //计算结果同样是字节数组
        bytHash = md5.ComputeHash(bytValue);
        //将字节数组转换为字符串
        string sTemp = "";
        for (int i = 0; i < bytHash.Length; i++)
        {
            sTemp += bytHash[i].ToString("x").PadLeft(2, '0');
        }
        return sTemp;
    }
}
posted @ 2013-11-10 11:10  不是豆豆  阅读(1022)  评论(1编辑  收藏  举报
友情链接:迷途