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");   

改为自己的

posted @ 2021-10-29 17:09  walleyekneel  阅读(379)  评论(0编辑  收藏  举报