Java new HmacUtilsHmacAlgorithmsHMAC_SHA_1 appSecrethmacHex 转C#
在C#中,可以使用System.Security.Cryptography.HMACSHA1类来实现HMAC-SHA1算法。以下是将Java代码转换为C#代码的示例:
using System;
using System.Security.Cryptography;
using System.Text;
public class Program
{
public static void Main(string[] args)
{
string appSecret = "your_app_secret";
string data = "your_data";
byte[] keyBytes = Encoding.UTF8.GetBytes(appSecret);
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
byte[] hmacBytes;
using (HMACSHA1 hmac = new HMACSHA1(keyBytes))
{
hmacBytes = hmac.ComputeHash(dataBytes);
}
string hmacHex = BitConverter.ToString(hmacBytes).Replace("-", "").ToLower();
Console.WriteLine(hmacHex);
}
}
在上述示例中,appSecret是你的应用密钥,data是要计算HMAC-SHA1的数据。首先,将appSecret和data转换为字节数组。然后,使用HMACSHA1类和密钥字节数组初始化HMAC对象,并使用ComputeHash方法计算HMAC-SHA1。最后,将计算得到的HMAC字节数组转换为十六进制字符串表示。
请记得替换your_app_secret和your_data为实际的应用密钥和数据
原文地址: https://www.cveoy.top/t/topic/hJKB 著作权归作者所有。请勿转载和采集!