Created
November 16, 2019 17:24
-
-
Save srsandy/80858aa93f49743ec4b5e8aafe0f8305 to your computer and use it in GitHub Desktop.
Encrypt and Decrypt emails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require('crypto'); | |
const algorithm = 'aes-192-cbc'; | |
const password = '3zTvzr3p67VC61jmV54rIYu1545x4TlY'; | |
const key = crypto.scryptSync(password, 'salt', 24); | |
const iv = Buffer.alloc(16, 0); | |
function encrypt(text) { | |
const cipher = crypto.createCipheriv(algorithm, key, iv) | |
let encrypted = cipher.update(text, 'utf8', 'hex') | |
encrypted += cipher.final('hex'); | |
return encrypted; | |
} | |
function decrypt(encrypted) { | |
const decipher = crypto.createDecipheriv(algorithm, key, iv) | |
let dec = decipher.update(encrypted, 'hex', 'utf8') | |
dec += decipher.final('utf8'); | |
return dec; | |
} | |
const email = "[email protected]"; | |
const hw = encrypt(email); | |
console.log(decrypt(hw)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment