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

94.59% Statements 35/37
87.5% Branches 14/16
100% Functions 1/1
94.59% Lines 35/37
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      1x   1x 1x   1x   1x       1x         1x 50x               76259x       76259x 33480x     42779x               33480x   33480x       33480x       1x   1x     1x 1x   1x     33480x   33480x 33480x 33480x 33480x   33480x 26367x   7113x 7113x     33480x     33480x   33480x       33480x 1x     33479x      
import { TUnicodeArrayCallsWrapper } from '../../types/custom-nodes/TUnicodeArrayCallsWrapper';
import { TUnicodeArrayNode } from '../../types/custom-nodes/TUnicodeArrayNode';
 
import { UnicodeArrayEncoding } from '../../enums/UnicodeArrayEncoding';
 
import { AbstractReplacer } from './AbstractReplacer';
import { NumberLiteralReplacer } from './NumberLiteralReplacer';
import { UnicodeArray } from '../../UnicodeArray';
import { Utils } from '../../Utils';
 
export class StringLiteralReplacer extends AbstractReplacer {
    /**
     * @type {number}
     */
    private static minimumLengthForUnicodeArray: 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 replaceWithUnicodeArrayFlag: boolean = (
            nodeValue.length >= StringLiteralReplacer.minimumLengthForUnicodeArray
            && Math.random() <= this.options.unicodeArrayThreshold
        );
 
        if (this.options.unicodeArray && replaceWithUnicodeArrayFlag) {
            return this.replaceStringLiteralWithUnicodeArrayCall(nodeValue);
        }
 
        return Utils.stringToUnicode(nodeValue);
    }
 
    /**
     * @param value
     * @returns {string}
     */
    private replaceStringLiteralWithUnicodeArrayCall (value: string): string {
        const unicodeArrayNode: TUnicodeArrayNode = <TUnicodeArrayNode>this.nodes.get('unicodeArrayNode');
 
        Iif (!unicodeArrayNode) {
            throw new ReferenceError('`unicodeArrayNode` node is not found in Map with custom nodes.');
        }
 
        let rc4Key: string = '';
 
        switch (this.options.unicodeArrayEncoding) {
            case UnicodeArrayEncoding.base64:
                value = Utils.btoa(value);
 
                break;
 
            case UnicodeArrayEncoding.rc4:
                rc4Key = Utils.getRandomGenerator().pickone(StringLiteralReplacer.rc4Keys);
                value = Utils.btoa(Utils.rc4(value, rc4Key));
 
                break;
        }
 
        value = Utils.stringToUnicode(value);
 
        let unicodeArray: UnicodeArray = unicodeArrayNode.getNodeData(),
            indexOfExistingValue: number = unicodeArray.getIndexOf(value),
            indexOfValue: number,
            hexadecimalIndex: string;
 
        if (indexOfExistingValue >= 0) {
            indexOfValue = indexOfExistingValue;
        } else {
            indexOfValue = unicodeArray.getLength();
            unicodeArrayNode.updateNodeData(value);
        }
 
        hexadecimalIndex = new NumberLiteralReplacer(this.nodes, this.options)
            .replace(indexOfValue);
 
        const unicodeArrayCallsWrapper: TUnicodeArrayCallsWrapper = <TUnicodeArrayCallsWrapper>this.nodes.get('unicodeArrayCallsWrapper');
 
        Iif (!unicodeArrayCallsWrapper) {
            throw new ReferenceError('`unicodeArrayCallsWrapper` node is not found in Map with custom nodes.');
        }
 
        if (this.options.unicodeArrayEncoding === UnicodeArrayEncoding.rc4) {
            return `${unicodeArrayCallsWrapper.getNodeIdentifier()}('${hexadecimalIndex}', ${Utils.stringToUnicode(rc4Key)})`;
        }
 
        return `${unicodeArrayCallsWrapper.getNodeIdentifier()}('${hexadecimalIndex}')`;
    }
}