This is a standard algorithm for the purposes of padding a block for a block
cipher. It will be used in CBC, which in turned will be used with AES for
ECIES.
This commit is contained in:
Ryan X. Charles 2014-08-24 19:38:20 -07:00
parent 1dead4cbc4
commit 1b1ecd989a
2 changed files with 35 additions and 0 deletions

14
lib/expmt/cbc.js Normal file
View File

@ -0,0 +1,14 @@
var Random = require('../random');
var CBC = function CBC() {
};
CBC.pkcs7pad = function(buf, blocksize) {
var bytesize = blocksize / 8;
var padbytesize = bytesize - buf.length;
var pad = new Buffer(padbytesize);
pad.fill(padbytesize);
return Buffer.concat([buf, pad]);
};
module.exports = CBC;

21
test/test.cbc.js Normal file
View File

@ -0,0 +1,21 @@
var should = require('chai').should();
var CBC = require('../lib/expmt/cbc');
describe('CBC', function() {
describe('@pkcs7pad', function() {
it('should pad this 32 bit buffer to 128 bits with the number 128/8 - 32/8', function() {
var buf = new Buffer(32 / 8);
buf.fill(0);
var padbuf = CBC.pkcs7pad(buf, 128);
padbuf.length.should.equal(128 / 8);
padbuf[32 / 8].should.equal(128 / 8 - 32 / 8);
padbuf[32 / 8 + 1].should.equal(128 / 8 - 32 / 8);
// ...
padbuf[32 / 8 + 128 / 8 - 32 / 8 - 1].should.equal(128 / 8 - 32 / 8);
});
});
});