xor buffers ... will be useful for CBC

This commit is contained in:
Ryan X. Charles 2014-08-24 20:51:56 -07:00
parent e097fe23ec
commit 38d9ab65af
2 changed files with 47 additions and 1 deletions

View File

@ -1,6 +1,11 @@
var Random = require('../random');
var CBC = function CBC() {
var CBC = function CBC(blockcipherf, keybuf, ivbuf) {
if (!(this instanceof CBC))
return new CBC(blockcipherf, keybuf, ivbuf);
this.blockcipherf = blockcipherf;
this.keybuf = keybuf;
this.ivbuf = ivbuf;
};
CBC.pkcs7pad = function(buf, blocksize) {
@ -11,4 +16,17 @@ CBC.pkcs7pad = function(buf, blocksize) {
return Buffer.concat([buf, pad]);
};
CBC.xorbufs = function(buf1, buf2) {
if (buf1.length !== buf2.length)
throw new Error('bufs must have the same length');
var buf = new Buffer(buf1.length);
for (var i = 0; i < buf1.length; i++) {
buf[i] = buf1[i] ^ buf2[i];
}
return buf;
};
module.exports = CBC;

View File

@ -2,6 +2,16 @@ var should = require('chai').should();
var CBC = require('../lib/expmt/cbc');
describe('CBC', function() {
it('should return a new CBC', function() {
var cbc = new CBC();
should.exist(cbc);
})
it('should return a new CBC when called without "new"', function() {
var cbc = new CBC();
should.exist(cbc);
});
describe('@pkcs7pad', function() {
@ -18,4 +28,22 @@ describe('CBC', function() {
});
describe('@xorbufs', function() {
it('should xor 1 and 0', function() {
var buf1 = new Buffer([1]);
var buf2 = new Buffer([0]);
var buf = CBC.xorbufs(buf1, buf2);
buf[0].should.equal(1);
});
it('should xor 1 and 1', function() {
var buf1 = new Buffer([1]);
var buf2 = new Buffer([1]);
var buf = CBC.xorbufs(buf1, buf2);
buf[0].should.equal(0);
});
});
});