diff --git a/lib/expmt/cbc.js b/lib/expmt/cbc.js new file mode 100644 index 0000000..bc4f060 --- /dev/null +++ b/lib/expmt/cbc.js @@ -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; diff --git a/test/test.cbc.js b/test/test.cbc.js new file mode 100644 index 0000000..e662feb --- /dev/null +++ b/test/test.cbc.js @@ -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); + }); + + }); + +});