当前位置: 代码迷 >> 综合 >> Md5与HMAC-SHA256
  详细解决方案

Md5与HMAC-SHA256

热度:77   发布时间:2023-11-26 16:02:34.0

文章目录

  • MD5签名算法
  • HMAC-SHA256签名算法

MD5签名算法

private string CalMd5(string str){
    var md5 = MD5.Create();var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str));var sb = new StringBuilder();foreach (byte b in bs){
    sb.Append(b.ToString("x2"));}//所有字符转为大写return sb.ToString().ToUpper();}

HMAC-SHA256签名算法

private  string CalcHMACSHA256Hash(string plaintext, string salt){
    string result = "";var enc = Encoding.Default;byte[]baText2BeHashed = enc.GetBytes(plaintext),baSalt = enc.GetBytes(salt);System.Security.Cryptography.HMACSHA256 hasher = new HMACSHA256(baSalt);byte[] baHashedText = hasher.ComputeHash(baText2BeHashed);result = string.Join("", baHashedText.ToList().Select(b => b.ToString("x2")).ToArray());return result;}