symmetric encryption convenience class

This commit is contained in:
Ryan X. Charles 2014-08-27 17:15:10 -07:00
parent ae02a878dd
commit 1cb2f900af
2 changed files with 106 additions and 0 deletions

33
lib/expmt/encryption.js Normal file
View File

@ -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;

73
test/test.encryption.js Normal file
View File

@ -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'));
});
});
});