37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace HL_FristAidPlatform_ChestPain_Interface_CCPC
|
|
{
|
|
public static class Encryption
|
|
{
|
|
/// <summary>
|
|
/// 加密
|
|
/// </summary>
|
|
/// <param name="message"></param>
|
|
/// <param name="secret"></param>
|
|
/// <returns></returns>
|
|
public static string HmacSHA256(string message, string secret)
|
|
{
|
|
secret = secret ?? "";
|
|
var encoding = new System.Text.UTF8Encoding();
|
|
|
|
//使用SecretKey作为签名密钥
|
|
byte[] keyByte = encoding.GetBytes(message);
|
|
//使用待签名字符串作为签名文本
|
|
byte[] secretBytes = encoding.GetBytes(secret);
|
|
|
|
using (var hmacsha256 = new HMACSHA256(keyByte))
|
|
{
|
|
byte[] hashmessage = hmacsha256.ComputeHash(secretBytes);
|
|
StringBuilder builder = new StringBuilder();
|
|
for (int i = 0; i < hashmessage.Length; i++)
|
|
{
|
|
builder.Append(hashmessage[i].ToString("x2"));
|
|
}
|
|
return builder.ToString();
|
|
}
|
|
}
|
|
}
|
|
}
|