diff --git a/dist/index.js b/dist/index.js index d0bf2fd2..5611e38c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -406,6 +406,20 @@ var RandomGeneratorUtils = function () { } (0, _createClass3.default)(RandomGeneratorUtils, null, [{ + key: "initializeRandomGenerator", + value: function initializeRandomGenerator(seed) { + if (seed !== 0) { + RandomGeneratorUtils.randomGenerator = new chance_1.Chance(seed); + } else { + RandomGeneratorUtils.randomGenerator = new chance_1.Chance(); + } + } + }, { + key: "clearRandomGenerator", + value: function clearRandomGenerator() { + RandomGeneratorUtils.randomVariableNameSet.clear(); + } + }, { key: "getRandomFloat", value: function getRandomFloat(min, max) { return RandomGeneratorUtils.getRandomGenerator().floating({ @@ -451,11 +465,6 @@ var RandomGeneratorUtils = function () { RandomGeneratorUtils.randomVariableNameSet.add(randomVariableName); return randomVariableName; } - }, { - key: "setRandomGenerator", - value: function setRandomGenerator(randomGenerator) { - RandomGeneratorUtils.randomGenerator = randomGenerator; - } }]); return RandomGeneratorUtils; }(); @@ -463,7 +472,6 @@ var RandomGeneratorUtils = function () { RandomGeneratorUtils.randomGeneratorPool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; RandomGeneratorUtils.randomGeneratorPoolWithNumbers = RandomGeneratorUtils.randomGeneratorPool + "0123456789"; RandomGeneratorUtils.randomVariableNameSet = new _set2.default(); -RandomGeneratorUtils.randomGenerator = new chance_1.Chance(); exports.RandomGeneratorUtils = RandomGeneratorUtils; /***/ }, @@ -1429,7 +1437,6 @@ if (!global._babelPolyfill && parseInt(process.version.split('.')[0], 10) < 4) { __webpack_require__(135); } var ServiceIdentifiers_1 = __webpack_require__(4); -var chance_1 = __webpack_require__(49); var InversifyContainerFacade_1 = __webpack_require__(58); var JavaScriptObfuscatorCLI_1 = __webpack_require__(57); var RandomGeneratorUtils_1 = __webpack_require__(9); @@ -1446,11 +1453,11 @@ var JavaScriptObfuscator = function () { var inversifyContainerFacade = new InversifyContainerFacade_1.InversifyContainerFacade(inputOptions); var options = inversifyContainerFacade.get(ServiceIdentifiers_1.ServiceIdentifiers.IOptions); - if (options.seed !== 0) { - RandomGeneratorUtils_1.RandomGeneratorUtils.setRandomGenerator(new chance_1.Chance(options.seed)); - } + RandomGeneratorUtils_1.RandomGeneratorUtils.initializeRandomGenerator(options.seed); var javaScriptObfuscator = inversifyContainerFacade.get(ServiceIdentifiers_1.ServiceIdentifiers.IJavaScriptObfuscator); - return javaScriptObfuscator.obfuscate(sourceCode); + var obfuscationResult = javaScriptObfuscator.obfuscate(sourceCode); + RandomGeneratorUtils_1.RandomGeneratorUtils.clearRandomGenerator(); + return obfuscationResult; } }, { key: "runCLI", @@ -6752,6 +6759,9 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace _this.stringLiteralHexadecimalIndexCache = new _map2.default(); _this.customNodeGroupStorage = customNodeGroupStorage; _this.stringArrayStorage = stringArrayStorage; + _this.rc4Keys = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().n(function () { + return RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().string({ length: 4 }); + }, 50); return _this; } @@ -6791,7 +6801,7 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace key = void 0; switch (this.options.stringArrayEncoding) { case StringArrayEncoding_1.StringArrayEncoding.rc4: - key = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().pickone(StringLiteralReplacer_1.rc4Keys); + key = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().pickone(this.rc4Keys); encodedValue = CryptUtils_1.CryptUtils.btoa(CryptUtils_1.CryptUtils.rc4(value, key)); break; case StringArrayEncoding_1.StringArrayEncoding.base64: @@ -6822,9 +6832,6 @@ var StringLiteralReplacer = StringLiteralReplacer_1 = function (_AbstractReplace return StringLiteralReplacer; }(AbstractReplacer_1.AbstractReplacer); StringLiteralReplacer.minimumLengthForStringArray = 3; -StringLiteralReplacer.rc4Keys = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().n(function () { - return RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().string({ length: 4 }); -}, 50); StringLiteralReplacer = StringLiteralReplacer_1 = tslib_1.__decorate([inversify_1.injectable(), tslib_1.__param(0, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.TCustomNodeGroupStorage)), tslib_1.__param(1, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.TStringArrayStorage)), tslib_1.__param(2, inversify_1.inject(ServiceIdentifiers_1.ServiceIdentifiers.IOptions)), tslib_1.__metadata("design:paramtypes", [Object, Object, Object])], StringLiteralReplacer); exports.StringLiteralReplacer = StringLiteralReplacer; var StringLiteralReplacer_1; diff --git a/src/JavaScriptObfuscator.ts b/src/JavaScriptObfuscator.ts index 25c155de..666d6a57 100644 --- a/src/JavaScriptObfuscator.ts +++ b/src/JavaScriptObfuscator.ts @@ -6,8 +6,6 @@ if (!(global)._babelPolyfill && parseInt(process.version.split('.')[0], 10) import { ServiceIdentifiers } from './container/ServiceIdentifiers'; -import { Chance } from 'chance'; - import { TInputOptions } from './types/options/TInputOptions'; import { IInversifyContainerFacade } from './interfaces/container/IInversifyContainerFacade'; @@ -29,14 +27,15 @@ export class JavaScriptObfuscator { const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade(inputOptions); const options: IOptions = inversifyContainerFacade.get(ServiceIdentifiers.IOptions); - if (options.seed !== 0) { - RandomGeneratorUtils.setRandomGenerator(new Chance(options.seed)); - } + RandomGeneratorUtils.initializeRandomGenerator(options.seed); const javaScriptObfuscator: IJavaScriptObfuscator = inversifyContainerFacade .get(ServiceIdentifiers.IJavaScriptObfuscator); + const obfuscationResult: IObfuscationResult = javaScriptObfuscator.obfuscate(sourceCode); - return javaScriptObfuscator.obfuscate(sourceCode); + RandomGeneratorUtils.clearRandomGenerator(); + + return obfuscationResult; } /** diff --git a/src/node-transformers/obfuscation-transformers/replacers/StringLiteralReplacer.ts b/src/node-transformers/obfuscation-transformers/replacers/StringLiteralReplacer.ts index c7eebd25..10f5523a 100644 --- a/src/node-transformers/obfuscation-transformers/replacers/StringLiteralReplacer.ts +++ b/src/node-transformers/obfuscation-transformers/replacers/StringLiteralReplacer.ts @@ -20,11 +20,15 @@ export class StringLiteralReplacer extends AbstractReplacer { */ private static readonly minimumLengthForStringArray: number = 3; + /** + * @type {IStorage} + */ + private readonly customNodeGroupStorage: IStorage; + /** * @type {string[]} */ - private static readonly rc4Keys: string[] = RandomGeneratorUtils.getRandomGenerator() - .n(() => RandomGeneratorUtils.getRandomGenerator().string({length: 4}), 50); + private readonly rc4Keys: string[]; /** * @type {Map} @@ -36,11 +40,6 @@ export class StringLiteralReplacer extends AbstractReplacer { */ private readonly stringLiteralHexadecimalIndexCache: Map = new Map(); - /** - * @type {IStorage} - */ - private readonly customNodeGroupStorage: IStorage; - /** * @type {IStorage} */ @@ -60,6 +59,9 @@ export class StringLiteralReplacer extends AbstractReplacer { this.customNodeGroupStorage = customNodeGroupStorage; this.stringArrayStorage = stringArrayStorage; + + this.rc4Keys = RandomGeneratorUtils.getRandomGenerator() + .n(() => RandomGeneratorUtils.getRandomGenerator().string({length: 4}), 50); } /** @@ -119,7 +121,7 @@ export class StringLiteralReplacer extends AbstractReplacer { switch (this.options.stringArrayEncoding) { case StringArrayEncoding.rc4: - key = RandomGeneratorUtils.getRandomGenerator().pickone(StringLiteralReplacer.rc4Keys); + key = RandomGeneratorUtils.getRandomGenerator().pickone(this.rc4Keys); encodedValue = CryptUtils.btoa(CryptUtils.rc4(value, key)); break; diff --git a/src/utils/RandomGeneratorUtils.ts b/src/utils/RandomGeneratorUtils.ts index 419d5c89..5ef0fe08 100644 --- a/src/utils/RandomGeneratorUtils.ts +++ b/src/utils/RandomGeneratorUtils.ts @@ -21,7 +21,22 @@ export class RandomGeneratorUtils { /** * @type {Chance.Chance | Chance.SeededChance} */ - private static randomGenerator: Chance.Chance | Chance.SeededChance = new Chance(); + private static randomGenerator: Chance.Chance | Chance.SeededChance; + + /** + * @param seed + */ + public static initializeRandomGenerator (seed: number): void { + if (seed !== 0) { + RandomGeneratorUtils.randomGenerator = new Chance(seed); + } else { + RandomGeneratorUtils.randomGenerator = new Chance(); + } + } + + public static clearRandomGenerator (): void { + RandomGeneratorUtils.randomVariableNameSet.clear(); + } /** * @param min @@ -94,11 +109,4 @@ export class RandomGeneratorUtils { return randomVariableName; } - - /** - * @param randomGenerator - */ - public static setRandomGenerator (randomGenerator: Chance.Chance | Chance.SeededChance): void { - RandomGeneratorUtils.randomGenerator = randomGenerator; - } } diff --git a/test/functional-tests/javascript-obfuscator/JavaScriptObfuscator.spec.ts b/test/functional-tests/javascript-obfuscator/JavaScriptObfuscator.spec.ts index 2da34d9c..c25be06f 100644 --- a/test/functional-tests/javascript-obfuscator/JavaScriptObfuscator.spec.ts +++ b/test/functional-tests/javascript-obfuscator/JavaScriptObfuscator.spec.ts @@ -1,7 +1,5 @@ import { assert } from 'chai'; -import { Chance } from 'chance'; - import { IObfuscationResult } from '../../../src/interfaces/IObfuscationResult'; import { JavaScriptObfuscator } from '../../../src/JavaScriptObfuscator'; @@ -15,7 +13,7 @@ import { RandomGeneratorUtils } from '../../../src/utils/RandomGeneratorUtils'; describe('JavaScriptObfuscator', () => { describe('obfuscate (sourceCode: string, customOptions?: IObfuscatorOptions): IObfuscationResult', () => { beforeEach(() => { - RandomGeneratorUtils.setRandomGenerator(new Chance()); + RandomGeneratorUtils.initializeRandomGenerator(0); }); describe('if `sourceMap` option is `false`', () => { @@ -155,9 +153,6 @@ describe('JavaScriptObfuscator', () => { let seed: number = 12345, equalsCount: number = 0; - //initial clear before testing - RandomGeneratorUtils.randomVariableNameSet.clear(); - for (let i: number = 0; i < samples; i++) { if (i % 20 === 0) { seed++; @@ -166,21 +161,15 @@ describe('JavaScriptObfuscator', () => { const obfuscationResult1: IObfuscationResult = JavaScriptObfuscator.obfuscate( code, { seed: seed } ); - RandomGeneratorUtils.randomVariableNameSet.clear(); - const obfuscationResult2: IObfuscationResult = JavaScriptObfuscator.obfuscate( code, { seed: seed } ); - RandomGeneratorUtils.randomVariableNameSet.clear(); if (obfuscationResult1.getObfuscatedCode() === obfuscationResult2.getObfuscatedCode()) { equalsCount++; } } - //clear after testing - RandomGeneratorUtils.randomVariableNameSet.clear(); - assert.equal(equalsCount, samples); }); @@ -206,7 +195,7 @@ describe('JavaScriptObfuscator', () => { }); afterEach(() => { - RandomGeneratorUtils.setRandomGenerator(new Chance()); + RandomGeneratorUtils.initializeRandomGenerator(0); }); }); });