Skip to content

Instantly share code, notes, and snippets.

@sagardere
Created May 23, 2018 13:52
Show Gist options
  • Save sagardere/86174d948d21fa4e5946bfd3b5d7010b to your computer and use it in GitHub Desktop.
Save sagardere/86174d948d21fa4e5946bfd3b5d7010b to your computer and use it in GitHub Desktop.
//Diffrent way to generate password in Nodejs
// 1
var CryptoJS = require("crypto-js");
var text = 'sagar';
var password = 'sagar';
var encrypted = CryptoJS.AES.encrypt(text, password);
encrypted = encrypted.toString();
console.log('@encrypted ' + encrypted);
var decrypted = CryptoJS.AES.decrypt(encrypted, password);
decrypted = decrypted.toString(CryptoJS.enc.Utf8)
console.log('@decrypted ' + decrypted);
//2
var passwordHash = require('password-hash');
var hashPassword = passwordHash.generate('sagar',{algorithm:"sha256"});
console.log('@hashPassword ' + hashPassword);
//3
var crypto = require('crypto');
function encrypt(data) {
var cipher = crypto.createCipher('aes-256-ecb', 'sagar');
return cipher.update(data, 'utf8', 'hex') + cipher.final('hex');
}
function decrypt(data) {
var cipher = crypto.createDecipher('aes-256-ecb', 'sagar');
return cipher.update(data, 'hex', 'utf8') + cipher.final('utf8');
}
var encryptPassword = encrypt('saaagar');
console.log('@encryptPassword ' + encryptPassword);
var decryptPassword = decrypt(encryptPassword);
console.log('@decryptPassword ' + decryptPassword);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment