SHA512在CryptoJS和Closure中不一样 [英] SHA512 not the same in CryptoJS and Closure

查看:212
本文介绍了SHA512在CryptoJS和Closure中不一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一些简单的加密挑战。

I have some troubles with a simple crypto-challenge.

我想做以下事情:


  • 获取url编码和base64编码的值

  • do url-decoding

  • do base64-decoding

  • 哈希与Sha512

  • getting a url-encoded and base64-encoded value
  • do url-decoding
  • do base64-decoding
  • hash with Sha512

使用CryptoJS时,我使用以下代码:

When working with CryptoJS, i use following code:

var parameter = "Akuwnm2318kwioasdjlnmn";
var urlDecoded = decodeURIComponent(parameter);
var base64Decoded = CryptoJS.enc.Base64.parse(urlDecoded);
var hashed = CryptoJS.SHA512(base64Decoded).toString(CryptoJS.enc.Base64);
//hashed = "UxupkI5+dkhUorQ+K3+Tqct1WNUkj3I6N76g82CbNQ0EAH/nWjqi9CW5Qec1vq/qakNIYeXeqiAPOVAVkzf9mA=="/eWTS2lUgCEe6NJDXhNfYvXMRQDvH6k2PHVmy6LJS7RloVvcQcpVjRNVU5lJpAg=="

使用Closure时,我使用以下代码:

When working with Closure, i use following code:

var parameter = "Akuwnm2318kwioasdjlnmn";
var urlDecoded = decodeURIComponent(parameter);
var byteArray = goog.crypt.base64.decodeStringToByteArray(urlDecoded);
var base64Decoded = goog.crypt.byteArrayToHex(byteArray);
var sha512 = new goog.crypt.Sha512();
sha512.update(base64Decoded);
var hashed = sha512.digest();
hashed = goog.crypt.byteArrayToHex(hashed);
//hashed = "bc2a878edfffb0937fbc6c0f9dbc9566edc59b74080d68d4c8bdfeb4027f17c4316a02285baaf446872d2df37b1144ac3ce18d62ab9c786b1f1fb18a53acea1d"

那么,为什么哈希会有所不同?

So, why are the hashes different?

如果有人能告诉我如何调整Closure-Code,我会很高兴得到与CryptoJS代码提供的哈希值相同。

I would be very happy if someone could tell me how to adapt the Closure-Code, to get the same hash as the CryptoJS code provides.

非常感谢!

PS:

我也尝试过:

var parameter = "Akuwnm2318kwioasdjlnmn";
var urlDecoded = decodeURIComponent(parameter);
var base64DecodedByteArray = goog.crypt.base64.decodeStringToByteArray(urlDecoded);
var sha512 = new goog.crypt.Sha512();
sha512.update(base64DecodedByteArray);
var hashed = sha512.digest();
hashed = goog.crypt.byteArrayToHex(hashed);
//hashed = "531ba9908e7e764854a2b43e2b7f93a9cb7558d5248f723a37bea0f3609b350d04007fe75a3aa2f425b941e735beafea6a434861e5deaa200f3950159337fd98"

然后,如你所见,我得到另一个哈希值。为什么??

but then, as you see, i get another hash. why??

推荐答案

第一个哈希值与第三个哈希值相同,除了它是base64编码而不是十六进制编码。您可以更改为十六进制编码并获得相同的值:

The first hash value is identical to the third, except it is base64-encoded rather than hex-encoded. You can change to hex encoding and get the same value:

var hashed = CryptoJS.SHA512(base64Decoded).toString(CryptoJS.enc.Hex);
//hashed = "531ba9908e7e764854a2b43e2b7f93a9cb7558d5248f723a37bea0f3609b350d04007fe75a3aa2f425b941e735beafea6a434861e5deaa200f3950159337fd98"

您展示的第二种方法具有不同的值,因为你没有散列相同的数据;您将byteArray转换为十六进制字符串,然后散列该字符串表示,而不是基础值。

The second approach you show has a different value because you are not hashing the same data; you are instead converting the byteArray to a hex string and then hashing that string representation, not the underlying values.

这篇关于SHA512在CryptoJS和Closure中不一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆