mirror of
https://gitee.com/fudiwei/DotNetCore.SKIT.FlurlHttpClient.Wechat.git
synced 2025-07-16 16:50:43 +08:00
refactor(openai): 优化哈希算法工具类
This commit is contained in:
parent
6511d9205c
commit
a7eaf61c01
@ -1,39 +1,44 @@
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities
|
||||
{
|
||||
using SKIT.FlurlHttpClient.Primitives;
|
||||
|
||||
/// <summary>
|
||||
/// SHA-1 算法工具类。
|
||||
/// </summary>
|
||||
public static class SHA1Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取 SHA-1 信息摘要。
|
||||
/// 计算 SHA-1 哈希值。
|
||||
/// </summary>
|
||||
/// <param name="bytes">信息字节数组。</param>
|
||||
/// <returns>信息摘要字节数组。</returns>
|
||||
/// <param name="bytes">要计算哈希值的信息字节数组。</param>
|
||||
/// <returns>哈希值字节数组。</returns>
|
||||
public static byte[] Hash(byte[] bytes)
|
||||
{
|
||||
if (bytes is null) throw new ArgumentNullException(nameof(bytes));
|
||||
|
||||
using SHA1 sha = SHA1.Create();
|
||||
return sha.ComputeHash(bytes);
|
||||
#if NET5_0_OR_GREATER
|
||||
return SHA1.HashData(bytes);
|
||||
#else
|
||||
using SHA1 sha1 = SHA1.Create();
|
||||
return sha1.ComputeHash(bytes);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取 SHA-1 信息摘要。
|
||||
/// 计算 SHA-1 哈希值。
|
||||
/// </summary>
|
||||
/// <param name="message">文本信息。</param>
|
||||
/// <returns>信息摘要。</returns>
|
||||
public static string Hash(string message)
|
||||
/// <param name="message">要计算哈希值的信息。</param>
|
||||
/// <returns>经过十六进制编码的哈希值。</returns>
|
||||
public static EncodedString Hash(string message)
|
||||
{
|
||||
if (message is null) throw new ArgumentNullException(nameof(message));
|
||||
|
||||
byte[] msgBytes = Encoding.UTF8.GetBytes(message);
|
||||
byte[] msgBytes = EncodedString.FromLiteralString(message);
|
||||
byte[] hashBytes = Hash(msgBytes);
|
||||
return BitConverter.ToString(hashBytes).Replace("-", string.Empty);
|
||||
return EncodedString.ToHexString(hashBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -233,7 +233,7 @@ namespace SKIT.FlurlHttpClient.Wechat.OpenAI.Utilities
|
||||
tmp.Sort(StringComparer.Ordinal);
|
||||
|
||||
string rawText = string.Join(string.Empty, tmp);
|
||||
string signText = SHA1Utility.Hash(rawText);
|
||||
string signText = SHA1Utility.Hash(rawText)!;
|
||||
return signText.ToLower();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user