'Typescript: Encryption using crypto module
I'm trying to encrypt a password using the build in crypto module. I've been using createCipher before which is now deprecated. So I was wondering if a way like this was still a good thing.
Old code:
hashPassword(pass: string) {
const cipher = crypto.createCipher('aes256', 'a pass');
let encrypted = cipher.update(pass, 'utf8', 'hex');
encrypted += cipher.final('hex');
return encrypted;
}
I appreciate any kind of suggestions.
Solution 1:[1]
use something like this from Jyotirmoy Upadhaya
const crypto = require("crypto");
const ENC = "bf3c199c2470cb477d907b1e0917c17b";
const IV = "5183666c72eec9e4";
const ALGO = "aes-256-cbc";
const encrypt = (text) => {
let cipher = crypto.createCipheriv(ALGO, ENC, IV);
let encrypted = cipher.update(text, "utf8", "base64");
encrypted += cipher.final("base64");
return encrypted;
};
const decrypt = (text) => {
let decipher = crypto.createDecipheriv(ALGO, ENC, IV);
let decrypted = decipher.update(text, "base64", "utf8");
return decrypted + decipher.final("utf8");
};
const encrypted_key = encrypt("HelloWorld");
const decrypted_key = decrypt(encrypted_key);
console.log(encrypted_key);
console.log(decrypted_key);
Solution 2:[2]
Check this... https://www.w3schools.com/nodejs/ref_crypto.asp
var crypto = require("crypto");
var mykey = crypto.createCipher("aes-128-cbc", "mypassword");
var mystr = mykey.update("abc", "utf8", "hex");
mystr += mykey.final("hex");
console.log(mystr);
but it's a weak method without using KEY and IV...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | stzia |
| Solution 2 |
