SHA256 is undefined

Paul Losev

I was experimenting with CryptoJS library and came across with the problem that my imported hash function isn't visible inside a class. Here's my code:

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());

[SOLVED] Thanks for answer!

Martín Zaragoza

In your code you've imported the CryptoJs module and the SHA256 function, but you've not imported the SHA512 function.

Try adding:

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

On top of the script

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related