C# HmacSha512 与 java HmacSha512 加密

时间:2023-03-10 07:14:15
C# HmacSha512 与 java HmacSha512 加密

C# HmacSha512 与 java HmacSha512 加密。

 /// <summary>
/// HmacSha512 加密
/// </summary>
/// <param name="clearMessage"></param>
/// <param name="secretKeyString"></param>
/// <returns></returns>
protected string HmacSha512(string clearMessage, string secretKeyString)
{
Encoding encoder = Encoding.UTF8; //Transform the clear query string to a byte array
byte[] messageBytes = encoder.GetBytes(clearMessage); //Transform the secret key string to a byte array
var key = Convert.ToBase64String(encoder.GetBytes(secretKeyString)); byte[] secretKeyBytes = encoder.GetBytes(key); //Init the Hmac SHA512 generator with the key
HMACSHA512 hmacsha512 = new HMACSHA512(secretKeyBytes); //Hash the message
byte[] hashValue = hmacsha512.ComputeHash(messageBytes); //Transform the hash bytes array to a string string
string hmac = BitConverter.ToString(hashValue).Replace("-", ""); //Force the case of the HMAC key to Uppercase
return hmac.ToLower();
}