【问题标题】:SHA256 is undefinedSHA256 未定义
【发布时间】:2018-08-22 18:21:03
【问题描述】:

我在试验 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());

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

【问题讨论】:

  • SHA256 已定义,但未定义 SHA512
  • 您正在导入SHA256,但使用的是SHA512。此外,您不应该在全局范围内声明变量。 (在您的情况下使用varconst 是首选)
  • 这里真正的问题是为什么你认为 SHA256 模块可以处理 SHA512 加密?记住,做一件事,但要把它做好……

标签: javascript node.js aes sha256 cryptojs


【解决方案1】:

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

尝试添加:

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

在脚本之上

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-18
    • 2021-12-11
    • 2010-10-21
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多