-
-
Save efureev/66b4ae26022f5559b4b4a5adaf32a98d to your computer and use it in GitHub Desktop.
Revisions
-
zmts revised this gist
Mar 19, 2018 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -27,4 +27,6 @@ function decrypt(text) { return decrypted.toString(); } module.exports = { decrypt, encrypt }; // http://vancelucas.com/blog/stronger-encryption-and-decryption-in-node-js/ -
vlucas created this gist
Jan 26, 2017 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ 'use strict'; const crypto = require('crypto'); const ENCRYPTION_KEY = process.env.ENCRYPTION_KEY; // Must be 256 bytes (32 characters) const IV_LENGTH = 16; // For AES, this is always 16 function encrypt(text) { let iv = crypto.randomBytes(IV_LENGTH); let cipher = crypto.createCipheriv('aes-256-cbc', new Buffer(ENCRYPTION_KEY), iv); let encrypted = cipher.update(text); encrypted = Buffer.concat([encrypted, cipher.final()]); return iv.toString('hex') + ':' + encrypted.toString('hex'); } function decrypt(text) { let textParts = text.split(':'); let iv = new Buffer(textParts.shift(), 'hex'); let encryptedText = new Buffer(textParts.join(':'), 'hex'); let decipher = crypto.createDecipheriv('aes-256-cbc', new Buffer(ENCRYPTION_KEY), iv); let decrypted = decipher.update(encryptedText); decrypted = Buffer.concat([decrypted, decipher.final()]); return decrypted.toString(); } module.exports = { decrypt, encrypt };