I have recently migrated a Blazor app to.net5 which no longer supports HmacSHA256 and hence I am trying to use jsInterop workaround to get it working. However, I see the value generated by CryptoJs HmacSHA256 is not the same as c#.
Javascript version with CryptoJS:
var getHmac = (privateKey, data) => {
const key = window.CryptoJS.enc.Utf8.parse(privateKey);
const utfData = window.CryptoJS.enc.Utf8.parse(data);
const hmac = window.CryptoJS.HmacSHA256(utfData, key);
return hmac;
}
const result = CryptoJS.enc.Hex.stringify(getHmac("123","abc");
And the c# version:
public byte[] HmacSha256(string key, string data)
{
var keyArray = Encoding.UTF8.GetBytes(key);
var hashAlgorithm = new HMACSHA256(keyArray);
return hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(data));
}
public string ToHexString(IReadOnlyCollection<byte> array)
{
var hex = new StringBuilder(array.Count * 2);
foreach (var b in array)
{
hex.AppendFormat("{0:x2}", b);
}
return hex.ToString();
}
var result = ToHexString(HmacSha256("123","abc"));
And they do not seem to have the same value. am I missing anything in the JavaScript implementation?
Thanks
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…