简体   繁体   English

SHA512在CryptoJS和Closure中不一样

[英]SHA512 not the same in CryptoJS and Closure

I have some troubles with a simple crypto-challenge. 我遇到了一些简单的加密挑战的麻烦。

I want to do following: 我想做以下事情:

  • getting a url-encoded and base64-encoded value 获取url编码和base64编码的值
  • do url-decoding 做网址解码
  • do base64-decoding 做base64解码
  • hash with Sha512 哈希与Sha512

When working with CryptoJS, i use following code: 使用CryptoJS时,我使用以下代码:

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=="

When working with Closure, i use following code: 使用Closure时,我使用以下代码:

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? 那么,为什么哈希不同?

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. 如果有人能告诉我如何调整Closure-Code,获得与CryptoJS代码提供的相同的哈希,我会很高兴。

Thanks a lot! 非常感谢!

PS: PS:

I also tried: 我也尝试过:

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?? 为什么??

The first hash value is identical to the third, except it is base64-encoded rather than hex-encoded. 第一个哈希值与第三个哈希值相同,除了它是base64编码而不是十六进制编码。 You can change to hex encoding and get the same value: 您可以更改为十六进制编码并获得相同的值:

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

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. 您将byteArray转换为十六进制字符串,然后散列该字符串表示,而不是基础值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM