using System;
using System.Security.Cryptography;
using System.Text;
namespace LrGetToken
{
///
/// SHA帮助类
///
public class SHAHelper
{
///
/// 实现HMAC SHA256签名算法
///
/// 加密秘钥
/// 加密内容
///
public static string HmacSHA256(string secret, string signContent)
{
string signRet = string.Empty;
using (HMACSHA256 mac = new HMACSHA256(Encoding.UTF8.GetBytes(secret)))
{
byte[] hash = mac.ComputeHash(Encoding.UTF8.GetBytes(signContent));
signRet = ToHexString(hash);
}
var hexBytes = Encoding.UTF8.GetBytes(signRet);
return Convert.ToBase64String(hexBytes);
}
///
/// 转16进制
///
///
///
protected 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;
}
}
}