将摘要+ hmacsha256 nodejs代码转换为c#

时间:2019-07-01 16:17:47

标签: hash sha256 hmac

我正在尝试将此Node.js代码重新创建为C#。但是,结果不符合期望值。我认为是因为我需要像nodejs一样实现摘要功能。但是我不知道摘要实际上是如何工作的

========================================

这是来自nodejs的代码段:

var shaBody = crypto.createHmac('SHA256', base64Key)
    .update(message);

var digestBody = shaBody.digest('base64');
console.log('Base64 Digest HMACSHA256 message: '+ digestBody);

========================================

这是C#代码:

byte[] secretkey = new Byte[64];
ASCIIEncoding encoding = new ASCIIEncoding();
string base64String = Convert.ToBase64String(encoding.GetBytes(authKey));
secretkey = Convert.FromBase64String(base64String);

byte[] requestBody = new Byte[64];
requestBody = 
Convert.FromBase64String(Convert.ToBase64String(encoding.GetBytes(body)));

using (HMACSHA256 hmac = new HMACSHA256(secretkey))
{
    byte[] hashValue = hmac.ComputeHash(requestBody);
    var hex = BitConverter.ToString(hashValue).Replace("-", 
"").ToLower();
    string requestSignatureBase64String = 
Convert.ToBase64String(hashValue);

    Console.WriteLine(base64String);
    Console.WriteLine(hex);
    Console.WriteLine(requestSignatureBase64String);
}

0 个答案:

没有答案