com.alicizax.unity/Runtime/Utility/Utility.Hash.HMACSha256.cs
陈思海 bf0d8340af init
2025-02-07 16:04:12 +08:00

41 lines
1.3 KiB
C#

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace AlicizaX.Runtime
{
public static partial class Utility
{
/// <summary>
/// 哈希计算相关的实用函数。
/// </summary>
public static partial class Hash
{
/// <summary>
/// HMACSha256
/// </summary>
public static class HMACSha256
{
/// <summary>
/// 使用提供的密钥对指定消息进行HMACSHA256哈希计算。
/// </summary>
/// <param name="message">要进行哈希计算的消息。</param>
/// <param name="key">用于哈希计算的密钥。</param>
/// <returns>Base64编码的哈希值。</returns>
public static string Hash(string message, string key)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
using (var hmac = new HMACSHA256(keyBytes))
{
byte[] hashBytes = hmac.ComputeHash(messageBytes);
return Convert.ToBase64String(hashBytes);
}
}
}
}
}
}