zoukankan      html  css  js  c++  java
  • C# HMAC_SHA1加密

    hmacsha1在很多签名计算中都很常用了,这里对两种可能返回的字符串类型做了分类 
    一种是直接返回字符串,一种是baset64后返回 
    需要看第三方对接文档中是否有特别说明,调试时如果报错,要比对串的内容看对方是否做了base64

    #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
  • 相关阅读:
    winform npoi excel 样式设置
    winform NPOI excel 导出并选择保存文件路径
    datagridview 代码添加列
    表单名 name 选择器
    NPOI 设置excel 边框
    winform 版本号比较
    winform app.cpnfig 文件的引用
    blog发布测试
    jQuery选择器
    表格隔行变色
  • 原文地址:https://www.cnblogs.com/yhnet/p/12448637.html
Copyright © 2011-2022 走看看