Net Core教程

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;
        }

 

这篇关于C# HMACSHA256加密的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!