HMACSHA256在C#VS的node.js
问题描述:
我生成的C#和Node.js的哈希字符串,但继续得到两种不同的结果...HMACSHA256在C#VS的node.js
在C#:
byte[] secretKeyBytes = Convert.FromBase64String('some_secret_key');
byte[] requestValuesSignature = Encoding.UTF8.GetBytes('teststring');
HMACSHA256 testHash = new HMACSHA256(secretKeyBytes);
byte[] = hashedBytes = testHash.ComputeHash(requestValuesSignature);
string hash1 = Convert.ToBAse64String(hashedBytes);
Console.WriteLine(hash1);
在Node.js的:
var hash2 = crypto.createHmac('SHA256', 'some_secret_key').update('teststring', 'utf8').digest('base64');
有人可以帮忙吗?
在此先感谢!
答
尝试这种方式
byte[] secretKeyBytes = Encoding.UTF8.GetBytes('some_secret_key');
因为JS实现不一样的,它只是会从钥匙串,这并不需要是一个base64字符串,但可以是任何字符串的字节数。
编辑:
正如你不能改变C#的一面,你必须做同样的(可能是错误的)行为上侧的NodeJS,即它被用作秘密之前解码的base64字符串。
var mykey = new Buffer('some_secret_key', 'base64').toString();
var hash2 = crypto.createHmac('SHA256', mykey).update('teststring', 'utf8').digest('base64');
要知道,双方将失败,如果秘密是不是一个有效的base64字符串
+0
我不能改变C#端的任何东西,因为它是来自另一个团队的代码..我不得不改变node.js端的东西.. –
+1
@ user1547174最好添加其他信息,比如这个问题。 – zaph
它可以帮助您提供结果,添加到问题中。 – zaph