本文介绍了HMAC C#和JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使C#和Javascript生成相同的HMAC时遇到麻烦:
Having trouble getting C# and Javascript to generate the same HMAC:
C#:
string data = String.Format("{0}{1}{2}{3}{4}{5}", APPId, requestHttpMethod, requestUri, requestTimeStamp, nonce, requestContentBase64String);
var secretKeyBytes = Convert.FromBase64String(sharedKey);
byte[] signature = Encoding.UTF8.GetBytes(data);
using (HMACSHA256 hmac = new HMACSHA256(secretKeyBytes))
{
byte[] signatureBytes = hmac.ComputeHash(signature);
return (incomingBase64Signature.Equals(Convert.ToBase64String(signatureBytes), StringComparison.Ordinal));
}
产生:apZUyGrS23BcEd2q5guGS4uQWVvcCvaDXIjCrLn / Hp4 =
Produces: apZUyGrS23BcEd2q5guGS4uQWVvcCvaDXIjCrLn/Hp4=
Javascript:
Javascript:
var signatureRawData = "".concat(appId, requestHttpMethod, requestUri, requestTimeStamp, nonce, requestContentBase64String);
var hash = CryptoJS.HmacSHA256(signatureRawData, apiKey);
var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);
产生:mFZyyKT03OOThRnt / 9dG / 0x + jRde3jCMvI6Rd0eKhEE =
Produces: mFZyyKT03OOThRnt/9dG/0x+jRde3jCMvI6Rd0eKhEE=
推荐答案
C#代码中的apiKey在哪里?它是sharedKey吗? sercretKeyBytes是字符串,char []还是byte []吗?我怀疑secrtetKeyBytes被转换为导致问题的字符串。
Where is the apiKey in the c# code? Is it sharedKey? Is sercretKeyBytes a string, char[], or byte[]? I suspect secrtetKeyBytes is being converted to a string which is the cause of the issue.
这篇关于HMAC C#和JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!