简体   繁体   English

使用CryptoJS使用字节[32]的Sha256?

[英]Sha256 with byte[32] using CryptoJS?

Using CryptoJS i got as a result a byte[8] when I need a 32 one, this code exactly: 使用CryptoJS时,当我需要32个字节时,我得到了一个字节[8],此代码正好是:

CryptoJS.SHA256(word);

How to get the 32? 如何获得32位?

This feels a bit convoluted, but I don't have a lot of experience with CryptoJS so perhaps there's a solution that requires less steps: 这让人有些费解,但是我对CryptoJS的经验不足,所以也许有一种解决方案需要更少的步骤:

const CryptoJS = require('crypto-js');

let hash   = CryptoJS.SHA256('hello world');
let buffer = Buffer.from(hash.toString(CryptoJS.enc.Hex), 'hex');
let array  = new Uint8Array(buffer);

If you need a proper JS array (one for which Array.isArray returns true ), you can use this: 如果你需要一个合适的JS数组(一个用于其Array.isArray返回true ),你可以这样做:

let array = Array.from( new Uint8Array(buffer) );

The solution was in my case: 解决方案是我的情况:

let utf16le = CryptoJS.enc.Utf16LE.parse(word);
let utf16Sha256 = CryptoJS.SHA256(utf16le);
return utf16Sha256.toString(CryptoJS.enc.Hex);

thanks to someone's else question 多亏别人的问题

CryptoJS.SHA256(word).toString()

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

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