Browse Source

unpad

patch-2
Ryan X. Charles 11 years ago
parent
commit
ae319ffcbc
  1. 15
      lib/expmt/cbc.js
  2. 12
      test/test.cbc.js

15
lib/expmt/cbc.js

@ -82,7 +82,20 @@ CBC.pkcs7pad = function(buf, blocksize) {
var padbytesize = bytesize - buf.length;
var pad = new Buffer(padbytesize);
pad.fill(padbytesize);
return Buffer.concat([buf, pad]);
var paddedbuf = Buffer.concat([buf, pad]);
return paddedbuf;
};
CBC.pkcs7unpad = function(paddedbuf, blocksize) {
var bytesize = blocksize / 8;
var padbytesize = bytesize - paddedbuf.length;
var padlength = paddedbuf[paddedbuf.length - 1];
var padbuf = paddedbuf.slice(paddedbuf.length - padlength, paddedbuf.length);
var padbuf2 = new Buffer(padlength);
padbuf2.fill(padlength);
if (padbuf.toString('hex') !== padbuf2.toString('hex'))
throw new Error('invalid padding');
return paddedbuf.slice(0, paddedbuf.length - padlength);
};
CBC.xorbufs = function(buf1, buf2) {

12
test/test.cbc.js

@ -176,6 +176,18 @@ describe('CBC', function() {
});
describe('@pkcs7unpad', function() {
it('should unpad this padded 32 bit buffer', function() {
var buf = new Buffer(32 / 8);
buf.fill(0);
var paddedbuf = CBC.pkcs7pad(buf, 128);
var unpaddedbuf = CBC.pkcs7unpad(paddedbuf, 128);
unpaddedbuf.toString('hex').should.equal(buf.toString('hex'));
});
});
describe('@xorbufs', function() {
it('should xor 1 and 0', function() {

Loading…
Cancel
Save