Last active
April 22, 2019 02:44
-
-
Save lcherone/a721e12a5d3d0a649c6e7b9d1b2e2d78 to your computer and use it in GitHub Desktop.
AES CBC Encryption helper nodejs
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"); | |
/** | |
* AES CBC Encryption helper | |
* | |
* Usage: | |
* ``` javascript | |
// | |
const encryption = require('@lib/encryption'); | |
// encrypt | |
let enc = encryption.encrypt({ | |
foo: 'some value', | |
bar: 'value xyz' | |
}, | |
process.env.ENCRYPTION_KEY || 'SecretKey123' | |
); | |
// decrypt | |
let dec = encryption.decrypt( | |
enc, | |
process.env.ENCRYPTION_KEY || 'SecretKey123' | |
); | |
// hash | |
let hashed = encryption.hash('sha512', 'hashme'); | |
// random string | |
let hashed = encryption.randomString(10); | |
``` | |
* | |
*/ | |
let Encryption = {}; | |
/** | |
* Create a random hex string using crypto.randomBytes | |
* - Encryption.randomString(10); | |
* | |
* @return {string} | |
*/ | |
Encryption.randomString = length => | |
crypto | |
.randomBytes(length) | |
.toString("hex") | |
.substr(0, length); | |
/** | |
* Create a hash | |
* - Encryption.hash('sha256', 'data to hash'); | |
* | |
* @return {Buffer} | |
*/ | |
Encryption.hash = (algo, data) => | |
crypto | |
.createHash(algo) | |
.update(data) | |
.digest(); | |
/** | |
* Encrypt | |
* - Encryption.encrypt(<string>, 'secret key'); | |
* | |
* @return {string} | |
*/ | |
Encryption.encrypt = (data, key) => { | |
let iv = crypto.randomBytes(16); | |
let cipher = crypto.createCipheriv( | |
"aes-256-cbc", | |
Encryption.hash("sha256", key), | |
iv | |
); | |
return ( | |
iv.toString("hex") + | |
":" + | |
Buffer.concat([ | |
cipher.update(JSON.stringify(data)), | |
cipher.final() | |
]).toString("hex") | |
); | |
}; | |
/** | |
* Decrypt data | |
* - Encryption.decrypt(<string>, 'secret key'); | |
* | |
* @return {mixed} | |
*/ | |
Encryption.decrypt = (data, key) => { | |
let parts = data.split(":"); | |
let iv = Buffer.from(parts.shift(), "hex"); | |
let decipher = crypto.createDecipheriv( | |
"aes-256-cbc", | |
Encryption.hash("sha256", key), | |
iv | |
); | |
return JSON.parse( | |
Buffer.concat([ | |
decipher.update(Buffer.from(parts.join(":"), "hex")), | |
decipher.final() | |
]).toString() | |
); | |
}; | |
module.exports = Encryption; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment