pkcs7-padding
A tiny package that handles PKCS #7 padding for both strings and byte arrays.
See RFC 5652, section 6.3 for more details.
Basic usage
var pkcs7 = require('pkcs7-padding');
var data = new Uint8Array(14);
var padded = pkcs7.pad(data);
var raw = pkcs7.unpad(padded);
Note that unpad()
throws an error when the padded data is incorrect :
try {
var raw = pkcs7.unpad(data);
} catch (e) {
console.error('this is not a valid PKCS #7 padded buffer');
}
It works with strings as well :
var pkcs7 = require('pkcs7-padding');
var padded = pkcs7.pad('this is a text');
var raw = pkcs7.unpad(padded);
Custom block size
You have the ability to change the block size of the padded data.
The default block size is set to 16 bytes.
The maximum block size is 255 bytes.
var pkcs7 = require('pkcs7-padding');
var data = new Uint8Array(14);
var padded = pkcs7.pad('this is a text', 32);
console.log(padded.byteLength);
var raw = pkcs7.unpad(padded);
console.log(raw.byteLength);