all files / core/security/encryptors/ normal-cipher.js

21.32% Statements 42/197
25.58% Branches 11/43
37.04% Functions 10/27
20.42% Lines 39/191
11 statements, 6 functions, 9 branches Ignored     
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258                                                                                                                                                                                                                                                                                                                                                                                                                                                     
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "./cipher"], function (require, exports, cipher_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _NormalCipherFour = (function (_super) {
        __extends(_NormalCipherFour, _super);
        function _NormalCipherFour(key) {
            var _this = _super.call(this) || this;
            _this._a = 0;
            _this._b = 0;
            var s = new Uint8Array(256);
            for (var i = 0; i < 256; ++i) {
                s[i] = i;
            }
            var keyLength = key.length;
            for (var i = 0, j = 0; i < 256; ++i) {
                var buffer = s[i];
                j = (j + buffer + key[i % keyLength]) & 0xff;
                s[i] = s[j];
                s[j] = buffer;
            }
            _this._s = s;
            return _this;
        }
        _NormalCipherFour.prototype._encryptBlock = function (data) {
            var a = this._a;
            var b = this._b;
            var s = this._s;
            var n = data.length;
            var output = new Uint8Array(n);
            for (var i = 0; i < n; ++i) {
                a = (a + 1) & 0xff;
                var first = s[a];
                b = (b + first) & 0xff;
                var second = s[b];
                s[a] = second;
                s[b] = first;
                output[i] = data[i] ^ s[(first + second) & 0xff];
            }
            this._a = a;
            this._b = b;
            return output;
        };
        _NormalCipherFour.prototype._decryptBlock = function (data) {
            return this._encryptBlock(data);
        };
        _NormalCipherFour.prototype._encrypt = function (data) {
            return this._encryptBlock(data);
        };
        return _NormalCipherFour;
    }(cipher_1._Cipher));
    exports._NormalCipherFour = _NormalCipherFour;
    var _NullCipher = (function (_super) {
        __extends(_NullCipher, _super);
        function _NullCipher() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        _NullCipher.prototype._decryptBlock = function (data) {
            return data;
        };
        _NullCipher.prototype._encrypt = function (data) {
            return data;
        };
        return _NullCipher;
    }(cipher_1._Cipher));
    exports._NullCipher = _NullCipher;
    var _CipherTwo = (function (_super) {
        __extends(_CipherTwo, _super);
        function _CipherTwo(key, effectiveKeyBits) {
            if (effectiveKeyBits === void 0) { effectiveKeyBits = 64; }
            var _this = _super.call(this) || this;
            _this._blockSize = 8;
            _this._effectiveKeyBits = 64;
            if (!(key instanceof Uint8Array) || key.length < 5 || key.length > 128) {
                throw new Error('RC2 key must be between 5 and 128 bytes.');
            }
            _this._key = key;
            _this._effectiveKeyBits = effectiveKeyBits;
            _this._expandedKey = _this._expandKey(key, _this._effectiveKeyBits);
            return _this;
        }
        _CipherTwo.prototype._expandKey = function (key, bits) {
            var piTable = [
                217, 120, 249, 196, 25, 221, 181, 237, 40, 233, 253, 121, 74, 160, 216, 157,
                198, 126, 55, 131, 43, 118, 83, 142, 98, 76, 100, 136, 68, 139, 251, 162,
                23, 154, 89, 245, 135, 179, 79, 19, 97, 69, 109, 141, 9, 129, 125, 50,
                189, 143, 64, 235, 134, 183, 123, 11, 240, 149, 33, 34, 92, 107, 78, 130,
                84, 214, 101, 147, 206, 96, 178, 28, 115, 86, 192, 20, 167, 140, 241, 220,
                18, 117, 202, 31, 59, 190, 228, 209, 66, 61, 212, 48, 163, 60, 182, 38,
                111, 191, 14, 218, 70, 105, 7, 87, 39, 242, 29, 155, 188, 148, 67, 3,
                248, 17, 199, 246, 144, 239, 62, 231, 6, 195, 213, 47, 200, 102, 30, 215,
                8, 232, 234, 222, 128, 82, 238, 247, 132, 170, 114, 172, 53, 77, 106, 42,
                150, 26, 210, 113, 90, 21, 73, 116, 75, 159, 208, 94, 4, 24, 164, 236,
                194, 224, 65, 110, 15, 81, 203, 204, 36, 145, 175, 80, 161, 244, 112, 57,
                153, 124, 58, 133, 35, 184, 180, 122, 252, 2, 54, 91, 37, 85, 151, 49,
                45, 93, 250, 152, 227, 138, 146, 174, 5, 223, 41, 16, 103, 108, 186, 201,
                211, 0, 230, 207, 225, 158, 168, 44, 99, 22, 1, 63, 88, 226, 137, 169,
                13, 56, 52, 27, 171, 51, 255, 176, 187, 72, 12, 95, 185, 177, 205, 46,
                197, 243, 219, 71, 229, 165, 156, 119, 10, 166, 32, 104, 254, 127, 193, 173
            ];
            var xKey = new Uint8Array(128);
            xKey.set(key);
            var len = key.length;
            if (len < 128) {
                var index = 0;
                var x_1 = xKey[len - 1];
                while (len < 128) {
                    x_1 = piTable[(x_1 + xKey[index++]) & 0xFF] & 0xFF;
                    xKey[len++] = x_1;
                }
            }
            var t = (bits + 7) >> 3;
            var mask = 0xFF >> (7 & -bits);
            var x = piTable[xKey[128 - t] & mask] & 0xFF;
            xKey[128 - t] = x;
            for (var i = 128 - t - 1; i >= 0; i--) {
                x = piTable[x ^ xKey[i + t]] & 0xFF;
                xKey[i] = x;
            }
            var newKey = new Uint16Array(64);
            for (var i = 0; i < 64; i++) {
                newKey[i] = xKey[2 * i] + (xKey[2 * i + 1] << 8);
            }
            return newKey;
        };
        _CipherTwo.prototype._rotateLeft = function (x, n) {
            return ((x << n) | (x >>> (16 - n))) & 0xFFFF;
        };
        _CipherTwo.prototype._rotateRight = function (x, n) {
            return ((x >>> n) | (x << (16 - n))) & 0xFFFF;
        };
        _CipherTwo.prototype._encryptBlock = function (block) {
            var R = new Uint16Array(4);
            for (var i = 0; i < 4; i++) {
                R[i] = block[2 * i] + (block[2 * i + 1] << 8);
            }
            var j = 0;
            for (var round = 0; round < 16; round++) {
                R[0] = this._rotateLeft((R[0] + (R[1] & ~R[3]) + (R[2] & R[3]) + this._expandedKey[j++]) & 0xFFFF, 1);
                R[1] = this._rotateLeft((R[1] + (R[2] & ~R[0]) + (R[3] & R[0]) + this._expandedKey[j++]) & 0xFFFF, 2);
                R[2] = this._rotateLeft((R[2] + (R[3] & ~R[1]) + (R[0] & R[1]) + this._expandedKey[j++]) & 0xFFFF, 3);
                R[3] = this._rotateLeft((R[3] + (R[0] & ~R[2]) + (R[1] & R[2]) + this._expandedKey[j++]) & 0xFFFF, 5);
                if (round === 4 || round === 10) {
                    R[0] = (R[0] + this._expandedKey[R[3] & 63]) & 0xFFFF;
                    R[1] = (R[1] + this._expandedKey[R[0] & 63]) & 0xFFFF;
                    R[2] = (R[2] + this._expandedKey[R[1] & 63]) & 0xFFFF;
                    R[3] = (R[3] + this._expandedKey[R[2] & 63]) & 0xFFFF;
                }
            }
            var encrypted = new Uint8Array(8);
            for (var i = 0; i < 4; i++) {
                encrypted[2 * i] = R[i] & 0xFF;
                encrypted[2 * i + 1] = R[i] >>> 8;
            }
            return encrypted;
        };
        _CipherTwo.prototype._decryptBlock = function (block) {
            var R = new Uint16Array(4);
            for (var i = 0; i < 4; i++) {
                R[i] = block[2 * i] + (block[2 * i + 1] << 8);
            }
            var j = 63;
            for (var round = 15; round >= 0; round--) {
                if (round === 4 || round === 10) {
                    R[3] = (R[3] - this._expandedKey[R[2] & 63]) & 0xFFFF;
                    R[2] = (R[2] - this._expandedKey[R[1] & 63]) & 0xFFFF;
                    R[1] = (R[1] - this._expandedKey[R[0] & 63]) & 0xFFFF;
                    R[0] = (R[0] - this._expandedKey[R[3] & 63]) & 0xFFFF;
                }
                R[3] = this._rotateRight(R[3], 5);
                R[3] = (R[3] - (R[0] & ~R[2]) - (R[1] & R[2]) - this._expandedKey[j--]) & 0xFFFF;
                R[2] = this._rotateRight(R[2], 3);
                R[2] = (R[2] - (R[3] & ~R[1]) - (R[0] & R[1]) - this._expandedKey[j--]) & 0xFFFF;
                R[1] = this._rotateRight(R[1], 2);
                R[1] = (R[1] - (R[2] & ~R[0]) - (R[3] & R[0]) - this._expandedKey[j--]) & 0xFFFF;
                R[0] = this._rotateRight(R[0], 1);
                R[0] = (R[0] - (R[1] & ~R[3]) - (R[2] & R[3]) - this._expandedKey[j--]) & 0xFFFF;
            }
            var decrypted = new Uint8Array(8);
            for (var i = 0; i < 4; i++) {
                decrypted[2 * i] = R[i] & 0xFF;
                decrypted[2 * i + 1] = R[i] >>> 8;
            }
            return decrypted;
        };
        _CipherTwo.prototype._encrypt = function (data) {
            var padLength = this._blockSize - (data.length % this._blockSize);
            var padded = new Uint8Array(data.length + padLength);
            padded.set(data);
            padded.fill(padLength, data.length);
            var result = [];
            for (var i = 0; i < padded.length; i += this._blockSize) {
                var block = padded.subarray(i, i + this._blockSize);
                result.push(this._encryptBlock(block));
            }
            var totalLength = result.reduce(function (sum, arr) { return sum + arr.length; }, 0);
            var output = new Uint8Array(totalLength);
            var offset = 0;
            for (var _i = 0, result_1 = result; _i < result_1.length; _i++) {
                var chunk = result_1[_i];
                output.set(chunk, offset);
                offset += chunk.length;
            }
            return output;
        };
        _CipherTwo.prototype._decrypt = function (data, iv) {
            var result = [];
            var previousBlock = iv;
            for (var i = 0; i < data.length; i += this._blockSize) {
                var block = data.subarray(i, i + this._blockSize);
                var decryptedBlock = this._decryptBlock(block);
                for (var j = 0; j < this._blockSize; j++) {
                    decryptedBlock[j] ^= previousBlock[j];
                }
                result.push(decryptedBlock);
                previousBlock = block;
            }
            var totalLength = result.reduce(function (sum, arr) { return sum + arr.length; }, 0);
            var decrypted = new Uint8Array(totalLength);
            var offset = 0;
            for (var _i = 0, result_2 = result; _i < result_2.length; _i++) {
                var chunk = result_2[_i];
                decrypted.set(chunk, offset);
                offset += chunk.length;
            }
            var padLength = decrypted[decrypted.length - 1];
            if (padLength > 0 && padLength <= this._blockSize) {
                var isValidPadding = true;
                for (var i = decrypted.length - padLength; i < decrypted.length; i++) {
                    if (decrypted[i] !== padLength) {
                        isValidPadding = false;
                        break;
                    }
                }
                if (isValidPadding) {
                    return decrypted.subarray(0, decrypted.length - padLength);
                }
            }
            return decrypted;
        };
        return _CipherTwo;
    }(cipher_1._Cipher));
    exports._CipherTwo = _CipherTwo;
});