Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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));
–
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.