zoukankan      html  css  js  c++  java
  • c#实现 sha224算法(java对应HMAC-SHA224)

    public static string Hex(byte[] data)
            {
                var sb = new StringBuilder();
                foreach (var t in data)
                {
                    sb.Append(t.ToString("X2"));
                }
    
                return sb.ToString();
            }
    
            /// <summary>
            /// 哈希计算
            /// </summary>
            /// <param name="data">输入字符串</param>
            /// <param name="key">密钥KEY</param>
            /// <param name="algorithm">密文算法,参考Algorithms.cs中提供的HMac algorithm</param>
            /// <returns>哈希值</returns>
            public static string HMacSHA224(string data, string key, string algorithm = YAlgorithms.HMacSHA224)
            {
                return Hex(Compute(data, key, algorithm));
            }
    
            /// <summary>
            /// 哈希计算
            /// </summary>
            /// <param name="data">输入字符串</param>
            /// <param name="key">密钥KEY</param>
            /// <param name="algorithm">密文算法,参考Algorithms.cs中提供的HMac algorithm</param>
            /// <returns>哈希值</returns>
            public static byte[] Compute(string data, string key, string algorithm = YAlgorithms.HMacSHA224)
            {
                var keyParameter = new Org.BouncyCastle.Crypto.Parameters.KeyParameter(Encoding.UTF8.GetBytes(key));
                var input = Encoding.UTF8.GetBytes(data);
                var mac = Org.BouncyCastle.Security.MacUtilities.GetMac(algorithm);
                mac.Init(keyParameter);
                mac.BlockUpdate(input, 0, input.Length);
                return Org.BouncyCastle.Security.MacUtilities.DoFinal(mac);
            }
    

      

    引入dll:BouncyCastle.dll

    调用:var data=

    HMacSHA224

    ("123456","keytest对应你们key");   

    改为自己的

  • 相关阅读:
    数据库三范式(转)
    Tyrion中文文档(含示例源码)
    mongodb数据库导入导出恢复
    HTTP协议:Content-Type
    requests爬虫组件
    JS 数组对象
    JS 函数
    javascript window对象属性和方法
    js String对象
    Math对象-JavaScript
  • 原文地址:https://www.cnblogs.com/walleyekneel/p/15481377.html
Copyright © 2011-2022 走看看