编程语言
首页 > 编程语言> > C# HMACSHA256加密

C# HMACSHA256加密

作者:互联网

C# HMACSHA256加密

using System.Security.Cryptography;
 
//加密算法HmacSHA256  
        private static string HmacSHA256(string secret, string signKey)
        {
            string signRet = string.Empty;
            using (HMACSHA256 mac = new HMACSHA256(Encoding.UTF8.GetBytes(signKey)))
            {
                byte[] hash = mac.ComputeHash(Encoding.UTF8.GetBytes(secret));
                signRet = Convert.ToBase64String(hash);
                //signRet = ToHexString(hash); ;
            }
            return signRet;
        }   
 
//byte[]转16进制格式string
        public static string ToHexString(byte[] bytes)
        {
            string hexString = string.Empty;
            if (bytes != null)
            {
                StringBuilder strB = new StringBuilder();
                foreach (byte b in bytes)
                {
                    strB.AppendFormat("{0:x2}", b);
                }
                hexString = strB.ToString();
            }
            return hexString;
        }

 

标签:加密,string,signRet,C#,HMACSHA256,strB,byte,hash
来源: https://www.cnblogs.com/wangwangwangMax/p/16352963.html