javascript - SHA256 未定义

标签 javascript node.js aes sha256 cryptojs

我正在尝试 CryptoJS 库,并遇到了我导入的哈希函数在类中不可见的问题。这是我的代码:

CryptoJS = require('crypto-js');
SHA256 = require('crypto-js/sha256');

class trCrypt {
  constructor(input,key) {
this.input = input;
this.key = SHA512(key).toString();
  }
  encrypt(){
    this.step1 = CryptoJS.AES.encrypt(this.input,this.key);
    return this.step1.toString()
  }
  decrypt(){
    const bytes =  CryptoJS.AES.decrypt(this.step1);
    this.dec1 = bytes.toString(CryptoJS.enc.Utf8);
    return this.dec1
  }
}
a = new trCrypt('hello','world');
console.log(a.encrypt());
console.log(a.decrypt());

[已解决]感谢您的回答!

最佳答案

在您的代码中,您已导入 CryptoJs 模块和 SHA256 函数,但尚未导入 SHA512 函数。

尝试添加:

SHA512 = require('crypto-js/sha512');

在脚本之上

关于javascript - SHA256 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51953592/

相关文章:

javascript - 在 Jquery 中使用 Match 只返回一个值

node.js - 什么是最佳实践 "joining"mongoose/mongodb 中没有填充的一堆值

Node.js 和 Socket.io - 如何使用 emit 函数发送数组

python - 在 Python 中使用 AES GCM 解密加密消息

java - AES key 长度无效 :39bytes

javascript - AngularJS ngRoute 抛出未捕获错误 : [$injector:modulerr]

javascript - angularjs ng-disabled 不会将禁用添加到按钮

javascript - ES6 Spread Operator 将特定键克隆或提取到另一个对象

sql-server - 在同一个外键上加入两次

javascript - CryptoJS 不正确的 AES 编码