@syncfusion/ej2-pdf
Version:
Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.
129 lines (128 loc) • 7.07 kB
JavaScript
import { _PdfRmdSigner } from './pdf-cipher-signer';
import { _NistObjectIdentifiers, _PdfCryptographicObjectIdentifier } from './pdf-object-identifiers';
/**
* Provides internal utilities for mapping algorithm names/OIDs and creating
* signer instances for supported signature mechanisms.
*
* @private
*/
var _PdfSignerUtilities = /** @class */ (function () {
function _PdfSignerUtilities() {
this._algorithms = new Map();
this._objectIdentifiers = new Map(); //eslint-disable-line
this._initializeAlgorithmMappings();
this._initializeObjectIdentifierMappings();
}
/**
* Populates the internal algorithm map with known labels and OIDs that normalize
* to canonical signer mechanisms (e.g., "SHA-256withRSA").
*
* @private
* @returns {void} This method does not return a value.
*/
_PdfSignerUtilities.prototype._initializeAlgorithmMappings = function () {
var algorithmIdentifiers = new _NistObjectIdentifiers();
var identifiers = new _PdfCryptographicObjectIdentifier();
this._algorithms.set('MD2WITHRSA', 'MD2withRSA');
this._algorithms.set('MD2WITHRSAENCRYPTION', 'MD2withRSA');
this._algorithms.set(identifiers._messageDigest2WithRonCipherEncryption.id, 'MD2withRSA');
this._algorithms.set(identifiers._raceEvaluationEncryption.id, 'RSA');
this._algorithms.set('SHA1WITHRSA', 'SHA-1withRSA');
this._algorithms.set('SHA1WITHRSAENCRYPTION', 'SHA-1withRSA');
this._algorithms.set(identifiers._secureHash1WithRonCipherEncryption.id, 'SHA-1withRSA');
this._algorithms.set('SHA-1WITHRSA', 'SHA-1withRSA');
this._algorithms.set('SHA256WITHRSA', 'SHA-256withRSA');
this._algorithms.set('SHA256WITHRSAENCRYPTION', 'SHA-256withRSA');
this._algorithms.set(identifiers._secureHash256WithRonCipherEncryption.id, 'SHA-256withRSA');
this._algorithms.set('SHA-256WITHRSA', 'SHA-256withRSA');
this._algorithms.set('SHA384WITHRSA', 'SHA-384withRSA');
this._algorithms.set('SHA384WITHRSAENCRYPTION', 'SHA-384withRSA');
this._algorithms.set(identifiers._secureHash384WithRonCipherEncryption.id, 'SHA-384withRSA');
this._algorithms.set('SHA-384WITHRSA', 'SHA-384withRSA');
this._algorithms.set('SHA512WITHRSA', 'SHA-512withRSA');
this._algorithms.set('SHA-512WITHRSA', 'SHA-512withRSA');
this._algorithms.set(identifiers._secureHash512WithRonCipherEncryption.id, 'SHA-512withRSA');
this._algorithms.set('SHA1WITHRSAANDMGF1', 'SHA-1withRSAandMGF1');
this._algorithms.set('SHA-1WITHRSAANDMGF1', 'SHA-1withRSAandMGF1');
this._algorithms.set('SHA1WITHRSA/PSS', 'SHA-1withRSAandMGF1');
this._algorithms.set('SHA-1WITHRSA/PSS', 'SHA-1withRSAandMGF1');
this._algorithms.set('SHA256WITHRSAANDMGF1', 'SHA-256withRSAandMGF1');
this._algorithms.set('SHA-256WITHRSAANDMGF1', 'SHA-256withRSAandMGF1');
this._algorithms.set('SHA256WITHRSA/PSS', 'SHA-256withRSAandMGF1');
this._algorithms.set('SHA-256WITHRSA/PSS', 'SHA-256withRSAandMGF1');
this._algorithms.set('SHA384WITHRSAANDMGF1', 'SHA-384withRSAandMGF1');
this._algorithms.set('SHA-384WITHRSAANDMGF1', 'SHA-384withRSAandMGF1');
this._algorithms.set('SHA384WITHRSA/PSS', 'SHA-384withRSAandMGF1');
this._algorithms.set('SHA-384WITHRSA/PSS', 'SHA-384withRSAandMGF1');
this._algorithms.set('SHA512WITHRSAANDMGF1', 'SHA-512withRSAandMGF1');
this._algorithms.set('SHA-512WITHRSAANDMGF1', 'SHA-512withRSAandMGF1');
this._algorithms.set('SHA512WITHRSA/PSS', 'SHA-512withRSAandMGF1');
this._algorithms.set('SHA-512WITHRSA/PSS', 'SHA-512withRSAandMGF1');
this._algorithms.set('DSAWITHSHA256', 'SHA-256withDSA');
this._algorithms.set('DSAWITHSHA-256', 'SHA-256withDSA');
this._algorithms.set('SHA256/DSA', 'SHA-256withDSA');
this._algorithms.set('SHA-256/DSA', 'SHA-256withDSA');
this._algorithms.set('SHA256WITHDSA', 'SHA-256withDSA');
this._algorithms.set('SHA-256WITHDSA', 'SHA-256withDSA');
this._algorithms.set(algorithmIdentifiers._digitalSignatureWithSecureHash256AlgorithmIdentifier.id, 'SHA-256withDSA');
this._algorithms.set('RIPEMD160WITHRSA', 'RIPEMD160withRSA');
this._algorithms.set('RIPEMD160WITHRSAENCRYPTION', 'RIPEMD160withRSA');
this._algorithms.set(algorithmIdentifiers._ronCipherWithRaceEvaluationAlgorithmIdentifier.id, 'RIPEMD160withRSA');
};
/**
* Populates the internal mapping from canonical mechanism names to their
* associated object identifier structures or OIDs.
*
* @private
* @returns {void} This method does not return a value.
*/
_PdfSignerUtilities.prototype._initializeObjectIdentifierMappings = function () {
var algorithmIdentifiers = new _NistObjectIdentifiers();
var identifiers = new _PdfCryptographicObjectIdentifier();
this._objectIdentifiers.set('SHA-1withRSA', identifiers._secureHash1WithRonCipherEncryption);
this._objectIdentifiers.set('SHA-256withRSA', identifiers._secureHash1WithRonCipherEncryption);
this._objectIdentifiers.set('SHA-384withRSA', identifiers._secureHash256WithRonCipherEncryption);
this._objectIdentifiers.set('SHA-512withRSA', identifiers._secureHash512WithRonCipherEncryption);
this._objectIdentifiers.set('RIPEMD160withRSA', algorithmIdentifiers._ronCipherWithRaceEvaluationAlgorithmIdentifier.id);
};
/**
* Resolves the requested algorithm label to a canonical mechanism and returns
* a signer implementation for that mechanism.
*
* @private
* @param {string} algorithm The requested algorithm label (case-insensitive; OIDs and variants supported).
* @returns {_ISigner} A signer corresponding to the canonical mechanism.
* @throws {Error} If the signer mechanism is not recognized.
*/
_PdfSignerUtilities.prototype._getSigner = function (algorithm) {
var lower = algorithm.toLowerCase();
var mechanism = algorithm;
var found = false;
this._algorithms.forEach(function (value, key) {
if (!found && key.toLowerCase() === lower) {
mechanism = value;
found = true;
}
});
if (mechanism === 'SHA-1withRSA') {
return new _PdfRmdSigner('sha1');
}
else if (mechanism === 'SHA-256withRSA') {
return new _PdfRmdSigner('sha256');
}
else if (mechanism === 'SHA-384withRSA') {
return new _PdfRmdSigner('sha384');
}
else if (mechanism === 'SHA-512withRSA') {
return new _PdfRmdSigner('sha512');
}
else if (mechanism === 'RIPEMD160withRSA') {
return new _PdfRmdSigner('md5');
}
else {
throw new Error("Signer " + algorithm + " not recognised.");
}
};
return _PdfSignerUtilities;
}());
export { _PdfSignerUtilities };