From 1cb2f900af100a9f92ab8b695b0fe3c62ca8e715 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Wed, 27 Aug 2014 17:15:10 -0700 Subject: [PATCH] symmetric encryption convenience class --- lib/expmt/encryption.js | 33 +++++++++++++++++++ test/test.encryption.js | 73 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 lib/expmt/encryption.js create mode 100644 test/test.encryption.js diff --git a/lib/expmt/encryption.js b/lib/expmt/encryption.js new file mode 100644 index 0000000..dbb271c --- /dev/null +++ b/lib/expmt/encryption.js @@ -0,0 +1,33 @@ +var AES = require('./aes'); +var CBC = require('./cbc'); +var Random = require('../random'); +var Hash = require('../hash'); + +var Encryption = function Encryption() { +}; + +Encryption.encrypt = function(messagebuf, passwordstr) { + var cipherkeybuf = Hash.sha256(new Buffer(passwordstr)); + return Encryption.encryptCipherkey(messagebuf, cipherkeybuf); +}; + +Encryption.decrypt = function(encbuf, passwordstr) { + var cipherkeybuf = Hash.sha256(new Buffer(passwordstr)); + return Encryption.decryptCipherkey(encbuf, cipherkeybuf); +}; + +Encryption.encryptCipherkey = function(messagebuf, cipherkeybuf, ivbuf) { + ivbuf = ivbuf || Random.getRandomBuffer(128 / 8); + var ctbuf = CBC.encrypt(messagebuf, ivbuf, AES, cipherkeybuf); + var encbuf = Buffer.concat([ivbuf, ctbuf]); + return encbuf; +}; + +Encryption.decryptCipherkey = function(encbuf, cipherkeybuf) { + var ivbuf = encbuf.slice(0, 128 / 8); + var ctbuf = encbuf.slice(128 / 8); + var messagebuf = CBC.decrypt(ctbuf, ivbuf, AES, cipherkeybuf); + return messagebuf; +}; + +module.exports = Encryption; diff --git a/test/test.encryption.js b/test/test.encryption.js new file mode 100644 index 0000000..2c49fd1 --- /dev/null +++ b/test/test.encryption.js @@ -0,0 +1,73 @@ +var should = require('chai').should(); +var Encryption = require('../lib/expmt/encryption'); + +describe('Encryption', function() { + + describe('@encrypt', function() { + + it('should return encrypt one block', function() { + var password = "password"; + var messagebuf = new Buffer(128 / 8 - 1); + messagebuf.fill(0); + var encbuf = Encryption.encrypt(messagebuf, password); + encbuf.length.should.equal(128 / 8 + 128 / 8); + }); + + }); + + describe('@decrypt', function() { + + it('should decrypt that which was encrypted', function() { + var password = "password"; + var messagebuf = new Buffer(128 / 8 - 1); + messagebuf.fill(0); + var encbuf = Encryption.encrypt(messagebuf, password); + var messagebuf2 = Encryption.decrypt(encbuf, password); + messagebuf2.toString('hex').should.equal(messagebuf.toString('hex')); + }); + + }); + + describe('@encryptCipherkey', function() { + + it('should return encrypt one block', function() { + var cipherkeybuf = new Buffer(256 / 8); + cipherkeybuf.fill(0x10); + var ivbuf = new Buffer(128 / 8); + ivbuf.fill(0); + var messagebuf = new Buffer(128 / 8 - 1); + messagebuf.fill(0); + var encbuf = Encryption.encryptCipherkey(messagebuf, cipherkeybuf, ivbuf); + encbuf.length.should.equal(128 / 8 + 128 / 8); + }); + + it('should return encrypt two blocks', function() { + var cipherkeybuf = new Buffer(256 / 8); + cipherkeybuf.fill(0x10); + var ivbuf = new Buffer(128 / 8); + ivbuf.fill(0); + var messagebuf = new Buffer(128 / 8); + messagebuf.fill(0); + var encbuf = Encryption.encryptCipherkey(messagebuf, cipherkeybuf, ivbuf); + encbuf.length.should.equal(128 / 8 + 128 / 8 + 128 / 8); + }); + + }); + + describe('@decryptCipherkey', function() { + + it('should decrypt that which was encrypted', function() { + var cipherkeybuf = new Buffer(256 / 8); + cipherkeybuf.fill(0x10); + var ivbuf = new Buffer(128 / 8); + ivbuf.fill(0); + var messagebuf = new Buffer(128 / 8); + messagebuf.fill(0); + var encbuf = Encryption.encryptCipherkey(messagebuf, cipherkeybuf, ivbuf); + var messagebuf2 = Encryption.decryptCipherkey(encbuf, cipherkeybuf); + messagebuf2.toString('hex').should.equal(messagebuf.toString('hex')); + }); + + }); + +});