@syncfusion/ej2-pdf
Version:
Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.
74 lines (73 loc) • 2.78 kB
JavaScript
import { _PdfMessageDigestAlgorithms } from './pdf-digest-algorithms';
import { _PdfSignerUtilities } from './signature-utilities';
/**
* Provides internal utilities for performing hash selection, key validation,
* and RSA-based signing operations using supported digest algorithms.
*
* @private
*/
var _PdfSignaturePrivateKey = /** @class */ (function () {
function _PdfSignaturePrivateKey(hashAlgorithm, key) {
this._key = key;
var alg = new _PdfMessageDigestAlgorithms();
var allowedDigest = alg._getAllowedDigests(hashAlgorithm);
this._hashAlgorithm = alg._getDigest(allowedDigest);
if (!key || this._isRonCipherKey(key)) {
this._encryptionAlgorithm = 'RSA';
}
else {
throw new Error('Invalid key type');
}
}
/**
* Determines whether the provided key is a valid RSA (Ron cipher) private key.
*
* @private
* @param {_ICipherParam} key The key to inspect for RSA-compatible fields.
* @returns {boolean} True if the key includes both 'modulus' and 'privateExponent'; otherwise, false.
*/
_PdfSignaturePrivateKey.prototype._isRonCipherKey = function (key) {
return 'modulus' in key && 'privateExponent' in key;
};
/**
* Generates a digital signature for the given byte data using the selected
* digest and RSA encryption algorithm.
*
* @private
* @param {Uint8Array} bytes The input byte sequence to sign.
* @returns {Uint8Array} The generated signature, or null if an error occurs.
*/
_PdfSignaturePrivateKey.prototype._sign = function (bytes) {
try {
var signMode = this._hashAlgorithm + "with" + this._encryptionAlgorithm;
var util = new _PdfSignerUtilities();
var signer = util._getSigner(signMode);
signer._initialize(true, this._key);
signer._blockUpdate(bytes, 0, bytes.length);
return signer._generateSignature();
}
catch (error) {
return null;
}
};
/**
* Gets the internal hash algorithm associated with this signing key.
*
* @private
* @returns {string} The normalized hash algorithm name (e.g., "SHA1", "SHA256").
*/
_PdfSignaturePrivateKey.prototype._getHashAlgorithm = function () {
return this._hashAlgorithm;
};
/**
* Gets the internal encryption algorithm used for signature generation.
*
* @private
* @returns {string} The encryption algorithm name (e.g., "RSA").
*/
_PdfSignaturePrivateKey.prototype._getEncryptionAlgorithm = function () {
return this._encryptionAlgorithm;
};
return _PdfSignaturePrivateKey;
}());
export { _PdfSignaturePrivateKey };