码迷,mamicode.com
首页 > 编程语言 > 详细

C# HmacSha512 与 java HmacSha512 加密

时间:2018-01-12 22:42:30      阅读:905      评论:0      收藏:0      [点我收藏+]

标签:style   encoding   sage   color   base64   protect   tco   class   ram   

C# HmacSha512 与 java HmacSha512 加密。

 /// <summary>
        /// HmacSha512 加密
        /// </summary>
        /// <param name="clearMessage"></param>
        /// <param name="secretKeyString"></param>
        /// <returns></returns>
        protected string HmacSha512(string clearMessage, string secretKeyString)
        {
            Encoding encoder = Encoding.UTF8;

            //Transform the clear query string to a byte array
            byte[] messageBytes = encoder.GetBytes(clearMessage);

            //Transform the secret key string to a byte array
            var key = Convert.ToBase64String(encoder.GetBytes(secretKeyString));

            byte[] secretKeyBytes = encoder.GetBytes(key);

            //Init the Hmac SHA512 generator with the key
            HMACSHA512 hmacsha512 = new HMACSHA512(secretKeyBytes);

            //Hash the message
            byte[] hashValue = hmacsha512.ComputeHash(messageBytes);

            //Transform the hash bytes array to a string string
            string hmac = BitConverter.ToString(hashValue).Replace("-", "");

            //Force the case of the HMAC key to Uppercase
            return hmac.ToLower();
        }

 

C# HmacSha512 与 java HmacSha512 加密

标签:style   encoding   sage   color   base64   protect   tco   class   ram   

原文地址:https://www.cnblogs.com/mycing/p/8277704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!