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