2025-03-28 09:49:56 +08:00

42 lines
1.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace EShang.Common
{
/// <summary>
/// SHA1签名相关方法
/// </summary>
public class SHA1Util
{
/// <summary>
/// SHA1签名
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string SHA1Signature(string input)
{
using (SHA1 sha1Hash = SHA1.Create())
{
// 将输入字符串转换为字节数组
byte[] inputBytes = Encoding.UTF8.GetBytes(input);
// 计算输入的 SHA1 哈希值
byte[] hashBytes = sha1Hash.ComputeHash(inputBytes);
// 将哈希字节数组转换为十六进制字符串
StringBuilder hashStringBuilder = new StringBuilder();
foreach (byte b in hashBytes)
{
hashStringBuilder.Append(b.ToString("x2"));
}
// 返回 SHA1 签名的十六进制字符串
return hashStringBuilder.ToString();
}
}
}
}