using System; using System.Security.Cryptography; namespace SKIT.FlurlHttpClient.Wechat.Api.Utilities { using SKIT.FlurlHttpClient.Primitives; /// /// HMAC 算法工具类。 /// public static class HMACUtility { /// /// 计算 HMAC-SHA-256 哈希值。 /// /// 密钥字节数组。 /// 要计算哈希值的信息字节数组。 /// 哈希值字节数组。 public static byte[] HashWithSHA256(byte[] keyBytes, byte[] msgBytes) { if (keyBytes is null) throw new ArgumentNullException(nameof(keyBytes)); if (msgBytes is null) throw new ArgumentNullException(nameof(msgBytes)); #if NET5_0_OR_GREATER return HMACSHA256.HashData(keyBytes, msgBytes); #else using HMAC hmac = new HMACSHA256(keyBytes); return hmac.ComputeHash(msgBytes); #endif } /// /// 计算 HMAC-SHA-256 哈希值。 /// /// 密钥。 /// 要计算哈希值的信息。 /// 经过十六进制编码的哈希值。 public static EncodedString HashWithSHA256(string key, string message) { if (key is null) throw new ArgumentNullException(nameof(key)); if (message is null) throw new ArgumentNullException(nameof(message)); byte[] keyBytes = EncodedString.FromLiteralString(key); byte[] msgBytes = EncodedString.FromLiteralString(message); byte[] hashBytes = HashWithSHA256(keyBytes, msgBytes); return EncodedString.ToHexString(hashBytes); } } }