【问题标题】:CryptoJS getting Sha256 hash as UTF-8 stringCryptoJS 将 Sha256 哈希作为 UTF-8 字符串
【发布时间】:2020-05-09 00:53:48
【问题描述】:

我正在使用 crypto-js 在 React Native 中进行哈希处理。

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

我正在尝试将crypto-js SHA256 哈希转换为UTF-8 字符串,以便我可以将它与库一起使用。但是,它一直说Malformed UTF-8 data

我不确定我是否根本无法将其转换为 UTF-8?这很奇怪,因为我已经能够成功地使用 Node 的加密模块来做到这一点 - 只是无法使用 crypto-js

这是我的代码:https://repl.it/repls/CornsilkPlasticSpof

另外,在此处添加以防止链接死亡,

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】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-26
      • 2018-03-09
      • 2016-04-03
      • 1970-01-01
      • 2012-03-25
      • 2016-08-24
      • 2021-11-19
      • 2018-03-30
      相关资源
      最近更新 更多