最近开发接口对接方使用hmacsha1来进行加密验证,特定找了文章查看。
https://www.cnblogs.com/yhnet/p/12448637.html
基本使用base64的比较多
原始版本
#region HMACSHA1加密 将二进制数据直接转为字符串返回 /// /// HMACSHA1加密 /// /// 要加密的原串 ///私钥 ///
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
base64版本
#region HMACSHA1加密 二进制数据转Base64后再返回 /// /// HMACSHA1加密 /// /// 要加密的原串 ///私钥 ///
public static string HMACSHA1Base64(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
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/205888.html原文链接:https://javaforall.net
