using System;
using System.Security.Cryptography;
using System.Text;
namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities
{
///
/// HMAC-SHA-256 算法工具类。
///
public static class HMACSHA256Utility
{
///
/// 获取信息摘要。
///
/// 密钥字节数组。
/// 信息字节数组。
/// 信息摘要。
public static string Hash(byte[] secretBytes, byte[] bytes)
{
if (secretBytes == null) throw new ArgumentNullException(nameof(secretBytes));
if (bytes == null) throw new ArgumentNullException(nameof(bytes));
using HMAC hmac = new HMACSHA256(secretBytes);
byte[] hashBytes = hmac.ComputeHash(bytes);
return BitConverter.ToString(hashBytes).Replace("-", "");
}
///
/// 获取信息摘要。
///
/// 密钥。
/// 文本信息。
/// 信息摘要。
public static string Hash(string secret, string message)
{
if (secret == null) throw new ArgumentNullException(nameof(secret));
if (message == null) throw new ArgumentNullException(nameof(message));
byte[] secretBytes = Encoding.UTF8.GetBytes(secret);
byte[] bytes = Encoding.UTF8.GetBytes(message);
return Hash(secretBytes, bytes);
}
}
}