【发布时间】:2014-03-14 07:21:03
【问题描述】:
我正在尝试在 C# 上复制 JavaScript 哈希,但我得到了不同的结果。 JavaScript 上的代码是:
var key = "35353535353535366363636363",
credentials = "web:web",
shaObj = new jsSHA(credentials, "ASCII"),
hash = shaObj.getHMAC(key, "HEX", "SHA-1", "HEX"); // key and generated hash are hex values
alert("Hash: " + hash);
它返回以下哈希:
60c9059c9be9bcd092e00eb7f03492fa3259f459
我正在尝试的 C# 代码是:
key = "35353535353535366363636363";
string credentials = "web:web";
var encodingCred = new System.Text.ASCIIEncoding();
var encodingKey = new System.Text.ASCIIEncoding();
byte[] keyByte = encodingKey.GetBytes(key);
byte[] credentialsBytes = encodingCred.GetBytes(credentials);
using (var hmacsha1 = new HMACSHA1(keyByte))
{
byte[] hashmessage = hmacsha1.ComputeHash(credentialsBytes);
string hash = BitConverter.ToString(hashmessage).Replace("-", string.Empty).ToLower();
Console.WriteLine("HASH: " + hash);
}
它返回以下哈希:
5f7d27b9b3ddee33f85f0f0d8df03540d9cdd48b
我怀疑问题可能是我将“密钥”作为 ASCII 而不是 HEX 传递。经过数小时的研究,我无法找出必要的更改以使其正常工作。非常感谢任何帮助。
【问题讨论】:
-
为什么要不同的哈希函数返回相同的东西?
-
@Magus API 不同,但功能相同:HMAC-SHA1。
-
我很确定他回答了问题中的问题。不同的输入不应该给出相同的输出...
标签: c# javascript hash