mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Literal replacer refactoring
This commit is contained in:
@@ -1,17 +1,33 @@
|
||||
import { inject, injectable, postConstruct } from 'inversify';
|
||||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
|
||||
|
||||
import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
|
||||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
||||
|
||||
import { TIdentifierNamesGeneratorFactory } from '../../types/container/generators/TIdentifierNamesGeneratorFactory';
|
||||
import { IIdentifierNamesGenerator } from '../../interfaces/generators/identifier-names-generators/IIdentifierNamesGenerator';
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
|
||||
import { ArrayStorage } from '../ArrayStorage';
|
||||
import { IArrayUtils } from '../../interfaces/utils/IArrayUtils';
|
||||
import { ICryptUtils } from '../../interfaces/utils/ICryptUtils';
|
||||
import { IEncodedValue } from '../../interfaces/IEncodedValue';
|
||||
import { IEscapeSequenceEncoder } from '../../interfaces/utils/IEscapeSequenceEncoder';
|
||||
import { IOptions } from '../../interfaces/options/IOptions';
|
||||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
|
||||
import { IStringArrayStorageItemData } from '../../interfaces/storages/string-array-storage/IStringArrayStorageItem';
|
||||
|
||||
import { StringArrayEncoding } from '../../enums/StringArrayEncoding';
|
||||
|
||||
import { MapStorage } from '../MapStorage';
|
||||
|
||||
@injectable()
|
||||
export class StringArrayStorage extends ArrayStorage <string> {
|
||||
export class StringArrayStorage extends MapStorage <string, IStringArrayStorageItemData> {
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
private static readonly rc4KeyLength: number = 4;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
private static readonly rc4KeysCount: number = 50;
|
||||
|
||||
/**
|
||||
* @type {number}
|
||||
*/
|
||||
@@ -22,28 +38,57 @@ export class StringArrayStorage extends ArrayStorage <string> {
|
||||
*/
|
||||
private readonly arrayUtils: IArrayUtils;
|
||||
|
||||
/**
|
||||
* @type {ICryptUtils}
|
||||
*/
|
||||
private readonly cryptUtils: ICryptUtils;
|
||||
|
||||
/**
|
||||
* @type {IEscapeSequenceEncoder}
|
||||
*/
|
||||
private readonly escapeSequenceEncoder: IEscapeSequenceEncoder;
|
||||
|
||||
/**
|
||||
* @type {IIdentifierNamesGenerator}
|
||||
*/
|
||||
private readonly identifierNamesGenerator: IIdentifierNamesGenerator;
|
||||
|
||||
/**
|
||||
* @type {string[]}
|
||||
*/
|
||||
private readonly rc4Keys: string[];
|
||||
|
||||
/**
|
||||
* @param {TIdentifierNamesGeneratorFactory} identifierNamesGeneratorFactory
|
||||
* @param {IArrayUtils} arrayUtils
|
||||
* @param {IRandomGenerator} randomGenerator
|
||||
* @param {IOptions} options
|
||||
* @param {ICryptUtils} cryptUtils
|
||||
* @param {IEscapeSequenceEncoder} escapeSequenceEncoder
|
||||
*/
|
||||
constructor (
|
||||
@inject(ServiceIdentifiers.Factory__IIdentifierNamesGenerator)
|
||||
identifierNamesGeneratorFactory: TIdentifierNamesGeneratorFactory,
|
||||
@inject(ServiceIdentifiers.IArrayUtils) arrayUtils: IArrayUtils,
|
||||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator,
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions
|
||||
@inject(ServiceIdentifiers.IOptions) options: IOptions,
|
||||
@inject(ServiceIdentifiers.ICryptUtils) cryptUtils: ICryptUtils,
|
||||
@inject(ServiceIdentifiers.IEscapeSequenceEncoder) escapeSequenceEncoder: IEscapeSequenceEncoder
|
||||
) {
|
||||
super(randomGenerator, options);
|
||||
|
||||
this.identifierNamesGenerator = identifierNamesGeneratorFactory(options);
|
||||
this.arrayUtils = arrayUtils;
|
||||
this.cryptUtils = cryptUtils;
|
||||
this.escapeSequenceEncoder = escapeSequenceEncoder;
|
||||
|
||||
this.rc4Keys = this.randomGenerator.getRandomGenerator()
|
||||
.n(
|
||||
() => this.randomGenerator.getRandomGenerator().string({
|
||||
length: StringArrayStorage.rc4KeyLength
|
||||
}),
|
||||
StringArrayStorage.rc4KeysCount
|
||||
);
|
||||
}
|
||||
|
||||
@postConstruct()
|
||||
@@ -61,18 +106,87 @@ export class StringArrayStorage extends ArrayStorage <string> {
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} rotationValue
|
||||
* @param {string} value
|
||||
* @returns {IStringArrayStorageItemData}
|
||||
*/
|
||||
public rotateArray (rotationValue: number): void {
|
||||
this.storage = this.arrayUtils.rotate(this.storage, rotationValue);
|
||||
public get (value: string): IStringArrayStorageItemData {
|
||||
return this.getOrSetIfDoesNotExist(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} rotationAmount
|
||||
*/
|
||||
public rotateStorage (rotationAmount: number): void {
|
||||
this.storage = new Map(
|
||||
this.arrayUtils.rotate(
|
||||
Array.from(this.storage.entries()),
|
||||
rotationAmount
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns {string}
|
||||
*/
|
||||
public toString (): string {
|
||||
return this.storage.map((value: string) => {
|
||||
return `'${value}'`;
|
||||
}).toString();
|
||||
return Array
|
||||
.from(this.storage.values())
|
||||
.map((item: IStringArrayStorageItemData) => {
|
||||
return `'${this.escapeSequenceEncoder.encode(
|
||||
item.encodedValue,
|
||||
this.options.unicodeEscapeSequence
|
||||
)}'`;
|
||||
}).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @returns {IStringArrayStorageItemData}
|
||||
*/
|
||||
private getOrSetIfDoesNotExist (value: string): IStringArrayStorageItemData {
|
||||
const { encodedValue, decodeKey }: IEncodedValue = this.getEncodedValue(value);
|
||||
const storedStringArrayStorageItemData: IStringArrayStorageItemData | undefined = this.storage.get(encodedValue);
|
||||
|
||||
if (storedStringArrayStorageItemData) {
|
||||
return storedStringArrayStorageItemData;
|
||||
}
|
||||
|
||||
const stringArrayStorageItemData: IStringArrayStorageItemData = {
|
||||
encodedValue,
|
||||
decodeKey,
|
||||
value,
|
||||
index: this.getLength()
|
||||
};
|
||||
|
||||
this.storage.set(encodedValue, stringArrayStorageItemData);
|
||||
|
||||
return stringArrayStorageItemData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
* @returns {IEncodedValue}
|
||||
*/
|
||||
private getEncodedValue (value: string): IEncodedValue {
|
||||
let encodedValue: string;
|
||||
let decodeKey: string | null = null;
|
||||
|
||||
switch (this.options.stringArrayEncoding) {
|
||||
case StringArrayEncoding.Rc4:
|
||||
decodeKey = this.randomGenerator.getRandomGenerator().pickone(this.rc4Keys);
|
||||
encodedValue = this.cryptUtils.btoa(this.cryptUtils.rc4(value, decodeKey));
|
||||
|
||||
break;
|
||||
|
||||
case StringArrayEncoding.Base64:
|
||||
encodedValue = this.cryptUtils.btoa(value);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
encodedValue = value;
|
||||
}
|
||||
|
||||
return { encodedValue, decodeKey };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user