Code coverage report for src/node-obfuscators/replacers/StringLiteralReplacer.ts

# All files / src/node-obfuscators/replacers StringLiteralReplacer.ts

94.74% Statements 36/38
88.89% Branches 16/18
100% Functions 1/1
94.74% Lines 36/38

    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

    1x

    1x
    1x

    1x

    1x

    1x

    1x
    50x

    75448x

    75448x
    32365x

    43083x

    32365x

    32365x

    32365x

    1x

    1x

    1x
    1x

    1x

    32365x
    32364x

    32365x
    32365x
    32365x
    32365x

    32365x
    26367x

    5998x
    5998x

    32365x

    32365x

    32365x

    32365x
    1x

    32364x

     import { TStringArrayCallsWrapper } from '../../types/custom-nodes/TStringArrayCallsWrapper';
    import { TStringArrayNode } from '../../types/custom-nodes/TStringArrayNode';

    import { StringArrayEncoding } from '../../enums/StringArrayEncoding';

    import { AbstractReplacer } from './AbstractReplacer';
    import { NumberLiteralReplacer } from './NumberLiteralReplacer';
    import { StringArray } from '../../StringArray';
    import { Utils } from '../../Utils';

    export class StringLiteralReplacer extends AbstractReplacer {
        /**
         * @type {number}
         */
        private static minimumLengthForStringArray: number = 3;

        /**
         * @type {string[]}
         */
        private static rc4Keys: string[] = Utils.getRandomGenerator()
            .n(() => Utils.getRandomGenerator().string({length: 4}), 50);

        /**
         * @param nodeValue
         * @returns {string}
         */
        public replace (nodeValue: string): string {
            const replaceWithStringArrayFlag: boolean = (
                nodeValue.length >= StringLiteralReplacer.minimumLengthForStringArray
                && Utils.getRandomFloat(0, 1) <= this.options.stringArrayThreshold
            );

            if (this.options.stringArray && replaceWithStringArrayFlag) {
                return this.replaceStringLiteralWithStringArrayCall(nodeValue);
            }

            return `'${Utils.stringToUnicodeEscapeSequence(nodeValue)}'`;
        }

        /**
         * @param value
         * @returns {string}
         */
        private replaceStringLiteralWithStringArrayCall (value: string): string {
            const stringArrayNode: TStringArrayNode = <TStringArrayNode>this.nodes.get('stringArrayNode');

            Iif (!stringArrayNode) {
                throw new ReferenceError('`stringArrayNode` node is not found in Map with custom node.');
            }

            let rc4Key: string = '';

            switch (this.options.stringArrayEncoding) {
                case StringArrayEncoding.base64:
                    value = Utils.btoa(value);

                    break;

                case StringArrayEncoding.rc4:
                    rc4Key = Utils.getRandomGenerator().pickone(StringLiteralReplacer.rc4Keys);
                    value = Utils.btoa(Utils.rc4(value, rc4Key));

                    break;
            }

            if (this.options.unicodeEscapeSequence) {
                value = Utils.stringToUnicodeEscapeSequence(value);
            }

            let stringArray: StringArray = stringArrayNode.getNodeData(),
                indexOfExistingValue: number = stringArray.getIndexOf(value),
                indexOfValue: number,
                hexadecimalIndex: string;

            if (indexOfExistingValue >= 0) {
                indexOfValue = indexOfExistingValue;
            } else {
                indexOfValue = stringArray.getLength();
                stringArrayNode.updateNodeData(value);
            }

            hexadecimalIndex = new NumberLiteralReplacer(this.nodes, this.options)
                .replace(indexOfValue);

            const stringArrayCallsWrapper: TStringArrayCallsWrapper = <TStringArrayCallsWrapper>this.nodes.get('stringArrayCallsWrapper');

            Iif (!stringArrayCallsWrapper) {
                throw new ReferenceError('`stringArrayCallsWrapper` node is not found in Map with custom node.');
            }

            if (this.options.stringArrayEncoding === StringArrayEncoding.rc4) {
                return `${stringArrayCallsWrapper.getNodeIdentifier()}('${hexadecimalIndex}', '${Utils.stringToUnicodeEscapeSequence(rc4Key)}')`;
            }

            return `${stringArrayCallsWrapper.getNodeIdentifier()}('${hexadecimalIndex}')`;
        }
    }