define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _PdfCryptographicEncoding = (function () {
function _PdfCryptographicEncoding(cipher) {
this._cipher = cipher;
}
_PdfCryptographicEncoding.prototype._getAlgorithmName = function () {
return this._cipher._getAlgorithmName() + "/PKCS1Padding";
};
_PdfCryptographicEncoding.prototype._getInputBlock = function () {
return this._isEncryption ? this._cipher._getInputBlock() - 10 : this._cipher._getInputBlock();
};
_PdfCryptographicEncoding.prototype._getOutputBlock = function () {
return this._isEncryption ? this._cipher._getOutputBlock() : this._cipher._getOutputBlock() - 10;
};
_PdfCryptographicEncoding.prototype._initialize = function (forEncryption, parameters) {
this._cipher._initialize(forEncryption, parameters);
this._isPrivateKey = parameters._isPrivate;
this._isEncryption = forEncryption;
};
_PdfCryptographicEncoding.prototype._processBlock = function (input, inOff, length) {
return this._isEncryption ?
this._encodeBlock(input, inOff, length) :
this._decodeBlock(input, inOff, length);
};
_PdfCryptographicEncoding.prototype._encodeBlock = function (input, inOff, inLen) {
if (inLen > this._getInputBlock()) {
throw new Error('Input data too large for PKCS#1 padding.');
}
var block = new Uint8Array(this._cipher._getInputBlock());
if (this._isPrivateKey) {
block[0] = 0x01;
for (var i = 1; i < block.length - inLen - 1; i++) {
block[i] = 0xFF;
}
}
else {
block[0] = 0x02;
for (var i = 1; i < block.length - inLen - 1; i++) {
var randomByte = void 0;
do {
randomByte = Math.floor(Math.random() * 255) + 1;
} while (randomByte === 0);
block[i] = randomByte;
}
}
block[block.length - inLen - 1] = 0x00;
block.set(input.subarray(inOff, inOff + inLen), block.length - inLen);
return this._cipher._processBlock(block, 0, block.length);
};
_PdfCryptographicEncoding.prototype._decodeBlock = function (input, inOff, inLen) {
var block = this._cipher._processBlock(input, inOff, inLen);
if (block.length < this._getOutputBlock()) {
throw new Error('Data block is truncated.');
}
var type = block[0];
if (type !== 1 && type !== 2) {
throw new Error("Invalid block type: " + type + ".");
}
var separatorIndex = -1;
for (var i = 1; i < block.length; i++) {
if (block[i] === 0x00) {
separatorIndex = i;
break;
}
if (type === 1 && block[i] !== 0xFF) {
throw new Error('Invalid PKCS#1 padding: bad padding byte.');
}
}
if (separatorIndex === -1 || separatorIndex < 9) {
throw new Error('Invalid PKCS#1 padding: separator not found or too short.');
}
return block.subarray(separatorIndex + 1);
};
return _PdfCryptographicEncoding;
}());
exports._PdfCryptographicEncoding = _PdfCryptographicEncoding;
});
|