我正在尝试 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/