all files / core/security/digital-signature/signature/ signature-privatekey.js

35.71% Statements 10/28
0% Branches 0/6
28.57% Functions 2/7
35.71% Lines 10/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                                                                  
define(["require", "exports", "./pdf-digest-algorithms", "./signature-utilities"], function (require, exports, pdf_digest_algorithms_1, signature_utilities_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfSignaturePrivateKey = (function () {
        function _PdfSignaturePrivateKey(hashAlgorithm, key) {
            this._key = key;
            var alg = new pdf_digest_algorithms_1._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');
            }
        }
        _PdfSignaturePrivateKey.prototype._isRonCipherKey = function (key) {
            return 'modulus' in key && 'privateExponent' in key;
        };
        _PdfSignaturePrivateKey.prototype._sign = function (bytes) {
            try {
                var signMode = this._hashAlgorithm + "with" + this._encryptionAlgorithm;
                var util = new signature_utilities_1._PdfSignerUtilities();
                var signer = util._getSigner(signMode);
                signer._initialize(true, this._key);
                signer._blockUpdate(bytes, 0, bytes.length);
                return signer._generateSignature();
            }
            catch (error) {
                return null;
            }
        };
        _PdfSignaturePrivateKey.prototype._getHashAlgorithm = function () {
            return this._hashAlgorithm;
        };
        _PdfSignaturePrivateKey.prototype._getEncryptionAlgorithm = function () {
            return this._encryptionAlgorithm;
        };
        return _PdfSignaturePrivateKey;
    }());
    exports._PdfSignaturePrivateKey = _PdfSignaturePrivateKey;
});