Skip to content

Instantly share code, notes, and snippets.

@fdelbos
Created October 29, 2017 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdelbos/39cbcc0bfcb0bb35d64b867b265e3576 to your computer and use it in GitHub Desktop.
Save fdelbos/39cbcc0bfcb0bb35d64b867b265e3576 to your computer and use it in GitHub Desktop.
a quick way to aes encrypt with node
const crypto = require('crypto');
const IV_LENGTH = 16;
class Encryption {
constructor(b64Key) {
this.key = Buffer.from(b64Key, 'base64');
}
encryptString(str) {
const buff = Buffer.from(str);
return this.encryptBuffer(buff).toString('base64');
}
decryptString(str) {
const buff = Buffer.from(str, 'base64');
return this.decryptBuffer(buff).toString();
}
encryptObj(obj) {
return this.encryptString(JSON.stringify(obj));
}
decryptObj(str) {
return JSON.parse(this.decryptString(str));
}
encryptBuffer(buff) {
const iv = crypto.randomBytes(IV_LENGTH);
const cipher = crypto.createCipheriv('aes-256-cbc', this.key, iv);
const encrypted = Buffer.concat([
iv,
cipher.update(buff),
cipher.final()
]);
return encrypted;
}
decryptBuffer(buff) {
const iv = Buffer.alloc(IV_LENGTH);
buff.copy(iv, 0, 0, IV_LENGTH);
const data = buff.slice(IV_LENGTH);
const decipher = crypto.createDecipheriv('aes-256-cbc', this.key, iv);
return Buffer.concat([
decipher.update(data),
decipher.final()
]);
}
}
const genB64Key = () => {
return crypto.randomBytes(32).toString('base64');
};
module.exports = {
Encryption,
genB64Key
};
process.env.NODE_ENV = 'test';
const chai = require('chai');
const R = require('ramda');
const {genB64Key, Encryption} = require('./encryption');
const expect = chai.expect;
const exampleJSON = `
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}`;
describe('encryption', () => {
let k1;
it('genB64EncryptionKey', () => {
k1 = genB64Key();
expect(R.isEmpty(k1.length)).to.be.false;
expect(Buffer.isEncoding('base64')).to.be.true;
expect(Buffer.byteLength(k1, 'base64')).to.equal(32);
});
it('genB64EncryptionKey gen a new key', () => {
const k2 = genB64Key();
const buffK2 = Buffer.from(k2, 'base64');
const buffK1 = Buffer.from(k1, 'base64');
expect(buffK1.equals(buffK2)).to.be.false;
expect(buffK2.equals(buffK2)).to.be.true;
});
context('Encryption class', () => {
let enc;
const testStr = ``;
it('creates a new Encryption instance', () => {
enc = new Encryption(k1);
expect(R.has('key', enc)).to.be.true;
});
it('encryptBuffer and decryptBuffer', () => {
const buff = Buffer.from(exampleJSON);
const encBuff = enc.encryptBuffer(buff);
// console.log(encBuff.toString('base64'));
expect(buff.equals(encBuff)).to.be.false;
const decBuff = enc.decryptBuffer(encBuff);
expect(buff.equals(decBuff)).to.be.true;
const decStr = decBuff.toString();
expect(decStr).to.equal(exampleJSON);
});
it('encrypt and decrypt a string', () => {
const res = enc.encryptString(exampleJSON);
expect(enc.decryptString(res)).to.equal(exampleJSON);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment