在C#中重新加密加密的hmac代码

问题描述 投票:-1回答:1

我试图从JavaScript,Node js代码重新组装C#中的代码。

我在Node中编写了完整的工作示例,以确定代码是否正常工作,但现在我遇到了在C#中查找等效函数的问题。

使用Node.js的JavaScript代码

var crypto = require('crypto');
var timestamp = Date.now() / 1000;
var what = timestamp + "hello";
var secret = "SGVsbG8gV29ybGQ="; 
var key = Buffer(secret, 'base64');
var hmac = crypto.createHmac('sha256', key);

hmac.update(what);
var t = hmac.digest('base64');    
console.log(t);

我只需要知道如何重新组装这些功能:

var key = Buffer(secret, 'base64');
var hmac = crypto.createHmac('sha256', key);

hmac.update(what);
javascript c# node.js
1个回答
0
投票

这是一个c#的例子。它包括一个功能。您可以将时间戳信息作为我假设的字符串传递,但可以使用任何字符串完成。

https://dotnetfiddle.net/eAZGfE

public static string HashString(string StringToHash, string HachKey)
{
    System.Text.UTF8Encoding myEncoder = new System.Text.UTF8Encoding();
    byte[] Key = myEncoder.GetBytes(HachKey);
    byte[] Text = myEncoder.GetBytes(StringToHash);
    System.Security.Cryptography.HMACSHA1 myHMACSHA1 = new System.Security.Cryptography.HMACSHA1(Key);
    byte[] HashCode = myHMACSHA1.ComputeHash(Text);
    string hash =  BitConverter.ToString(HashCode).Replace("-", "");
    return hash.ToLower();
}
© www.soinside.com 2019 - 2024. All rights reserved.