'Crypto.JS HMAC update
I have some javascript code that I need to convert to C#. I'm struggling with this section. Does C# have a equivalent to this section of code?
function sign(key, content) {
var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, CryptoJS.enc.Base64.parse(key));
hmac.update("".concat(content.timestamp, "|"));
hmac.update(CryptoJS.enc.Base64.parse(content.nonce));
hmac.update('|');
return CryptoJS.enc.Base64.stringify(hmac.finalize());
}
I've started a method in C#, but I don't know how to address the 'update()' method being used above. I believe it refers to progressive hashing, but I'm not certain what that means.
UPDATE* I've created this snippet of code and it works! Is there a more elegant way to do this task?
using (var hmacsha256 = new HMACSHA256(keyBytes))
{
byte[] tmp = new byte[2000];
hmacsha256.TransformBlock(Encoding.UTF8.GetBytes(s1), 0, s1.Length, tmp, 0);
hmacsha256.TransformBlock(s2, 0, s2.Length, tmp, s1.Length);
hmacsha256.TransformBlock(Encoding.UTF8.GetBytes(s3), 0, s3.Length, tmp, s1.Length + s2.Length);
hmacsha256.TransformBlock(Encoding.UTF8.GetBytes(s4), 0, s4.Length, tmp, s1.Length + s2.Length + s3.Length);
var final = hmacsha256.TransformFinalBlock(tmp, 0, s1.Length + s2.Length + s3.Length + s4.Length);
var hash = hmacsha256.ComputeHash(final);
Console.WriteLine("Expected Result : 7qJ74WZJFpSzozuXAxQQeTsdEpZDDwBcR4+PZ4glkPY=");
Console.WriteLine("Actual Result : " + Convert.ToBase64String(hash));
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
