Sha256 with byte[32] using CryptoJS?

谁说我不能喝 提交于 2019-12-12 04:02:08

问题


Using CryptoJS i got as a result a byte[8] when I need a 32 one, this code exactly:

CryptoJS.SHA256(word);

How to get the 32?


回答1:


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:

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:

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



回答2:


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




回答3:


CryptoJS.SHA256(word).toString()


来源:https://stackoverflow.com/questions/44796371/sha256-with-byte32-using-cryptojs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!