crypto.js HMAC更新
我有一些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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论