在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
hmacsha1在很多签名计算中都很常用了,这里对两种可能返回的字符串类型做了分类
#region HMACSHA1加密 将二进制数据直接转为字符串返回 /// <summary> /// HMACSHA1加密 /// </summary> /// <param name="text">要加密的原串</param> ///<param name="key">私钥</param> /// <returns></returns> public static string HMACSHA1Text(string text,string key) { //HMACSHA1加密 HMACSHA1 hmacsha1 = new HMACSHA1(); hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(key); byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(text); byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer); var enText = new StringBuilder(); foreach (byte iByte in hashBytes) { enText.AppendFormat("{0:x2}", iByte); } return enText.ToString(); } #endregion
#region HMACSHA1加密 对二进制数据转Base64后再返回 /// <summary> /// HMACSHA1加密 /// </summary> /// <param name="text">要加密的原串</param> ///<param name="key">私钥</param> /// <returns></returns> public static string HMACSHA1Text(string text,string key) { //HMACSHA1加密 HMACSHA1 hmacsha1 = new HMACSHA1(); hmacsha1.Key = System.Text.Encoding.UTF8.GetBytes(key); byte[] dataBuffer = System.Text.Encoding.UTF8.GetBytes(text); byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer); return Convert.ToBase64String(hashBytes); } #endregion
|
请发表评论