all files / core/security/digital-signature/signature/ pdf-cipher-signer.js

22.08% Statements 17/77
0% Branches 0/42
14.29% Functions 2/14
22.08% Lines 17/77
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                                                                                                                                                                                                      
define(["require", "exports", "../../encryptors/secureHash-algorithm256", "../../encryptors/secureHash-algorithm1", "./algorithm-handler", "./cryptographic-encoder", "./pdf-accumulator", "./pdf-digest-handler", "../../encryptors/secureHash-algorithm512", "../../encryptors/evaluation-digest"], function (require, exports, secureHash_algorithm256_1, secureHash_algorithm1_1, algorithm_handler_1, cryptographic_encoder_1, pdf_accumulator_1, pdf_digest_handler_1, secureHash_algorithm512_1, evaluation_digest_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfRmdSigner = (function () {
        function _PdfRmdSigner(digest) {
            this._isSigning = false;
            this._algorithmName = digest;
            this._digest = this._getDigest(digest);
            this._output = new pdf_accumulator_1._PdfNativeAccumulatorSink();
            this._input = this._digest._startChunkedConversion(this._output);
            this._ronCipherEngine = new cryptographic_encoder_1._PdfCryptographicEncoding(new algorithm_handler_1._PdfRsaAlgorithm());
            this._id = this._getAlgorithmIdentifier(digest);
        }
        _PdfRmdSigner.prototype._getMap = function () {
            if (!this._map) {
                this._map = new Map();
                this._initializeMap();
            }
            return this._map;
        };
        _PdfRmdSigner.prototype._initializeMap = function () {
            this._map.set('sha1', '1.3.14.3.2.26');
            this._map.set('sha256', '2.16.840.1.101.3.4.2.1');
            this._map.set('sha384', '2.16.840.1.101.3.4.2.2');
            this._map.set('sha512', '2.16.840.1.101.3.4.2.3');
            this._map.set('md5', '1.3.36.3.2.1');
        };
        _PdfRmdSigner.prototype._getDigest = function (digest) {
            var normalizedDigest = digest.toLowerCase();
            switch (normalizedDigest) {
                case 'sha1':
                case 'sha-1':
                    return new secureHash_algorithm1_1._Sha1();
                case 'sha256':
                case 'sha-256':
                    return new secureHash_algorithm256_1._Sha256();
                case 'sha384':
                case 'sha-384':
                    return new secureHash_algorithm512_1._Sha384();
                case 'sha512':
                case 'sha-512':
                    return new secureHash_algorithm512_1._Sha512();
                case 'md5':
                case 'md-5':
                    return new evaluation_digest_1._RaceEvaluationMessageDigest();
                default:
                    throw new Error("Invalid digest algorithm: " + digest);
            }
        };
        _PdfRmdSigner.prototype._getAlgorithmIdentifier = function (digest) {
            var normalizedDigest = digest.toLowerCase();
            var oid = this._getMap().get(normalizedDigest);
            if (!oid) {
                throw new Error("Unsupported digest: " + digest);
            }
            return new pdf_accumulator_1._PdfNativeAlgorithmIdentifier(oid);
        };
        _PdfRmdSigner.prototype._initialize = function (isSigning, parameters) {
            this._isSigning = isSigning;
            if (isSigning && parameters && !parameters.privateExponent) {
                throw new Error('Private key required for signing.');
            }
            if (!isSigning && parameters && parameters.privateExponent) {
                throw new Error('Public key required for verification.');
            }
            this._reset();
            this._ronCipherEngine._initialize(isSigning, parameters);
        };
        _PdfRmdSigner.prototype._blockUpdate = function (input, inOff, length) {
            var data = input.subarray(inOff, inOff + length);
            this._input._add(data);
        };
        _PdfRmdSigner.prototype._generateSignature = function () {
            if (!this._isSigning) {
                throw new Error('Invalid operation: not in signing mode');
            }
            this._input._close();
            var hash = this._output._getResult();
            if (!hash || hash.length === 0) {
                return null;
            }
            var data = this._derEncode(hash);
            return this._ronCipherEngine._processBlock(data, 0, data.length);
        };
        _PdfRmdSigner.prototype._derEncode = function (hash) {
            if (!this._id) {
                return hash;
            }
            else if (this._id && hash && hash.length > 0) {
                return new pdf_digest_handler_1._PdfDigestInformation(this._id, hash)._getUniqueEncoded();
            }
            return new Uint8Array(0);
        };
        _PdfRmdSigner.prototype._reset = function () {
            this._output = new pdf_accumulator_1._PdfNativeAccumulatorSink();
            this._input = this._digest._startChunkedConversion(this._output);
        };
        _PdfRmdSigner.prototype._update = function (input) {
            this._input._add(new Uint8Array([input]));
        };
        _PdfRmdSigner.prototype._compareArrays = function (a, b) {
            if (a.length !== b.length) {
                return false;
            }
            for (var i = 0; i < a.length; i++) {
                if (a[i] !== b[i]) {
                    return false;
                }
            }
            return true;
        };
        return _PdfRmdSigner;
    }());
    exports._PdfRmdSigner = _PdfRmdSigner;
});