添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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));
                Concatenate the parts and use ComputeHash() or apply progressive hashing with TransformBlock() (corresponds to update()) and TransformFinalBlock() (corresponds to finalize()).
– Topaco
                May 24, 2022 at 17:41
                @Topaco  Thank you for the response. I'm finding that I can't just concatenate the strings like you suggest because of the way CryptoJS is handling the nonce. For example this line: CryptoJS.enc.Base64.parse(context.nonce); is different when you process it with hmac.update() verse just a string concatenation.  Here is an example of what I'm referencing. jsfiddle.net/keroger2k/0tqc396b/169
– Jon19992
                May 24, 2022 at 20:03
                The differences you find are caused by encoding bugs because you are concatenating strings and WordArrays, which implicitly performs conversions. When these bugs are fixed, results match: jsfiddle.net/de2htk7w. However, progressive hashing improves readability, and since C# supports it, you should use it after all.
– Topaco
                May 24, 2022 at 20:52
                @Topaco thank you for helping out. I've got some working (ugly) code!  Any suggestions on making this a bit more elegant would be greatly appreciated.
– Jon19992
                May 25, 2022 at 1:22
                @Topaco There is a class called IncrementalHash which would probably make the code more readable; here is the best factory method to use. TransformBlock are these implementation specific functions, they make for extremely ugly code; for a cipher you'd use streaming instead.
– Maarten Bodewes - on strike
                May 25, 2022 at 9:35
        

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.