all files / core/security/digital-signature/signature/ pdf-accumulator.js

28.77% Statements 21/73
0% Branches 0/10
28.57% Functions 4/14
28.77% Lines 21/73
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                                                                                                                                                              
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfNativeAccumulatorSink = (function () {
        function _PdfNativeAccumulatorSink() {
            this._events = [];
        }
        _PdfNativeAccumulatorSink.prototype._add = function (event) {
            this._events.push(event);
            this._result = event.bytes;
        };
        _PdfNativeAccumulatorSink.prototype._setResult = function (result) {
            this._result = result;
            this._events = [{ bytes: result }];
        };
        _PdfNativeAccumulatorSink.prototype._getResult = function () {
            if (this._events.length > 0) {
                return this._events[this._events.length - 1].bytes;
            }
            return this._result;
        };
        return _PdfNativeAccumulatorSink;
    }());
    exports._PdfNativeAccumulatorSink = _PdfNativeAccumulatorSink;
    var _PdfNativeHashInput = (function () {
        function _PdfNativeHashInput(hasher, outputSink) {
            this._buffer = [];
            this._closed = false;
            this._hasher = hasher;
            this._outputSink = outputSink;
        }
        _PdfNativeHashInput.prototype._add = function (data) {
            if (this._closed) {
                throw new Error('Cannot add data to closed hash input');
            }
            for (var i = 0; i < data.length; i++) {
                this._buffer.push(data[i]);
            }
        };
        _PdfNativeHashInput.prototype._close = function () {
            if (this._closed) {
                return;
            }
            this._closed = true;
            if (this._buffer.length > 0) {
                var inputData = new Uint8Array(this._buffer);
                var result = this._hasher._hash(inputData, 0, inputData.length);
                this._outputSink._add({ bytes: result });
            }
        };
        return _PdfNativeHashInput;
    }());
    exports._PdfNativeHashInput = _PdfNativeHashInput;
    var _PdfNativeAlgorithmIdentifier = (function () {
        function _PdfNativeAlgorithmIdentifier(oid) {
            this._oid = oid;
        }
        _PdfNativeAlgorithmIdentifier.prototype._getEncoded = function () {
            var oidBytes = this._encodeObjectIdentifier(this._oid);
            var nullBytes = new Uint8Array([0x05, 0x00]);
            var contentLength = oidBytes.length + nullBytes.length;
            var result = [];
            result.push(0x30);
            result.push(contentLength);
            result.push.apply(result, Array.from(oidBytes));
            result.push.apply(result, Array.from(nullBytes));
            return new Uint8Array(result);
        };
        _PdfNativeAlgorithmIdentifier.prototype._encodeObjectIdentifier = function (oidString) {
            var parts = oidString.split('.').map(Number);
            var bytes = [];
            bytes.push(parts[0] * 40 + parts[1]);
            for (var i = 2; i < parts.length; i++) {
                var value = parts[i];
                if (value < 128) {
                    bytes.push(value);
                }
                else {
                    var temp = [];
                    while (value > 0) {
                        temp.unshift(value & 0x7F);
                        value = Math.floor(value / 128);
                    }
                    for (var j = 0; j < temp.length - 1; j++) {
                        temp[j] |= 0x80;
                    }
                    bytes.push.apply(bytes, temp);
                }
            }
            var result = [];
            result.push(0x06);
            result.push(bytes.length);
            result.push.apply(result, bytes);
            return new Uint8Array(result);
        };
        return _PdfNativeAlgorithmIdentifier;
    }());
    exports._PdfNativeAlgorithmIdentifier = _PdfNativeAlgorithmIdentifier;
});