Reputation: 1089
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!
Upvotes: 0
Views: 6594
Reputation: 1817
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
Upvotes: 2