crypto.js HMAC更新

发布于 2025-02-01 00:52:31 字数 1405 浏览 2 评论 0原文

我有一些JavaScript代码需要转换为C#。我在本节上挣扎。 C#是否等同于这一部分代码?

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());
}

我在C#中启动了一种方法,但是我不知道如何解决上面使用的“ update()”方法。我相信这是指渐进的哈希,但我不确定这是什么意思。

u pdate* 我已经创建了这个代码段,它有效!是否有更优雅的方法来完成这项任务?

        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));
        }

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));
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文