CryptoJS获取Sha256哈希值为UTF-8字符串

问题描述 投票:0回答:1

我在React Native中使用crypto-js做哈希。

我有一些代码使用不同的库,它可以使用Ed25519密钥来签署字符串或二进制数据。

我试图获得 crypto-js SHA256哈希值转换为 UTF-8 字符串,这样我就可以使用它的库。但是,它一直说 Malformed UTF-8 data

我不知道是不是完全不能转换为UTF-8?这很奇怪,因为我已经能够使用Node的crypto模块成功地做到这一点 - 只是无法使用 crypto-js

这是我的代码。https:/repl.itreplsCornsilkPlasticSpof。

另外,在这里补充一下,防止链接死亡。

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

const dataToBeHashed = "testing";

const toBeSigned = CryptoJS.SHA256(dataToBeHashed).toString(CryptoJS.enc.Utf8);

console.log(toBeSigned);

这使得

Error: Malformed UTF-8 data
    at Object.stringify (/home/runner/CornsilkPlasticSpof/node_modules/crypto-js/core.js:513:24)
    at WordArray.init.toString (/home/runner/CornsilkPlasticSpof/node_modules/crypto-js/core.js:268:38)
    at /home/runner/CornsilkPlasticSpof/index.js:5:52
    at Script.runInContext (vm.js:131:20)
    at Object.<anonymous> (/run_dir/interp.js:156:20)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:
react-native cryptography cryptojs
1个回答
0
投票

哈希实际上是随机字节。 这些随机字节不太可能是格式良好的UTF-8字符。 你需要做的是将你的字节转换为UTF-8的Base64。

© www.soinside.com 2019 - 2024. All rights reserved.