HMAC C# 和 JavaScript
HMAC C# and JavaScript
无法让 C# 和 Javascript 生成相同的 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=
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=
c#代码中的apiKey在哪里?是共享密钥吗? sercretKeyBytes 是字符串、char[] 还是 byte[]?我怀疑 secrtetKeyBytes 正在转换为字符串,这是问题的原因。
无法让 C# 和 Javascript 生成相同的 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=
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=
c#代码中的apiKey在哪里?是共享密钥吗? sercretKeyBytes 是字符串、char[] 还是 byte[]?我怀疑 secrtetKeyBytes 正在转换为字符串,这是问题的原因。