diff --git a/dist/index.js b/dist/index.js index 7a4be24d..5495c423 100644 --- a/dist/index.js +++ b/dist/index.js @@ -153,7 +153,7 @@ var RandomGeneratorUtils = function () { _createClass(RandomGeneratorUtils, null, [{ key: "getRandomFloat", value: function getRandomFloat(min, max) { - return RandomGeneratorUtils.randomGenerator.floating({ + return RandomGeneratorUtils.getRandomGenerator().floating({ min: min, max: max, fixed: 7 @@ -171,7 +171,7 @@ var RandomGeneratorUtils = function () { }, { key: "getRandomInteger", value: function getRandomInteger(min, max) { - return RandomGeneratorUtils.randomGenerator.integer({ + return RandomGeneratorUtils.getRandomGenerator().integer({ min: min, max: max }); @@ -195,9 +195,9 @@ var RandomGeneratorUtils = function () { return "" + prefix + Utils_1.Utils.decToHex(RandomGeneratorUtils.getRandomInteger(rangeMinInteger, rangeMaxInteger)).substr(0, length); } }, { - key: "setRandomGeneratorSeed", - value: function setRandomGeneratorSeed(randomGeneratorSeed) { - RandomGeneratorUtils.randomGenerator = new chance_1.Chance(randomGeneratorSeed); + key: "setRandomGenerator", + value: function setRandomGenerator(randomGenerator) { + RandomGeneratorUtils.randomGenerator = randomGenerator; } }]); @@ -944,6 +944,7 @@ if (!global._babelPolyfill) { __webpack_require__(114); } var ServiceIdentifiers_1 = __webpack_require__(1); +var chance_1 = __webpack_require__(115); var InversifyContainerFacade_1 = __webpack_require__(41); var JavaScriptObfuscatorCLI_1 = __webpack_require__(40); var RandomGeneratorUtils_1 = __webpack_require__(2); @@ -961,7 +962,7 @@ 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.setRandomGeneratorSeed(options.seed); + RandomGeneratorUtils_1.RandomGeneratorUtils.setRandomGenerator(new chance_1.Chance(options.seed)); } var javaScriptObfuscator = inversifyContainerFacade.get(ServiceIdentifiers_1.ServiceIdentifiers.IJavaScriptObfuscator); return javaScriptObfuscator.obfuscate(sourceCode); @@ -1312,12 +1313,12 @@ var CryptUtils = function () { } _createClass(CryptUtils, null, [{ - key: 'btoa', + key: "btoa", value: function btoa(string) { var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var output = ''; string = encodeURIComponent(string).replace(/%([0-9A-F]{2})/g, function (match, p1) { - return String.fromCharCode(parseInt('' + Utils_1.Utils.hexadecimalPrefix + p1)); + return String.fromCharCode(parseInt("" + Utils_1.Utils.hexadecimalPrefix + p1)); }); for (var block, charCode, idx = 0, map = chars; string.charAt(idx | 0) || (map = '=', idx % 1); output += map.charAt(63 & block >> 8 - idx % 1 * 8)) { charCode = string.charCodeAt(idx += 3 / 4); @@ -1329,7 +1330,7 @@ var CryptUtils = function () { return output; } }, { - key: 'hideString', + key: "hideString", value: function hideString(str, length) { var escapeRegExp = function escapeRegExp(s) { return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); @@ -1347,18 +1348,18 @@ var CryptUtils = function () { } return result; }; - var randomString = RandomGeneratorUtils_1.RandomGeneratorUtils.randomGenerator.string({ + var randomString = RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().string({ length: length, pool: RandomGeneratorUtils_1.RandomGeneratorUtils.randomGeneratorPool }); var randomStringDiff = randomString.replace(new RegExp('[' + escapeRegExp(str) + ']', 'g'), ''); var randomStringDiffArray = randomStringDiff.split(''); - RandomGeneratorUtils_1.RandomGeneratorUtils.randomGenerator.shuffle(randomStringDiffArray); + RandomGeneratorUtils_1.RandomGeneratorUtils.getRandomGenerator().shuffle(randomStringDiffArray); randomStringDiff = randomStringDiffArray.join(''); return [randomMerge(str, randomStringDiff), randomStringDiff]; } }, { - key: 'rc4', + key: "rc4", value: function rc4(string, key) { var s = [], j = 0, diff --git a/src/JavaScriptObfuscator.ts b/src/JavaScriptObfuscator.ts index 0f0179d4..1573d541 100644 --- a/src/JavaScriptObfuscator.ts +++ b/src/JavaScriptObfuscator.ts @@ -6,6 +6,8 @@ if (!(global)._babelPolyfill) { import { ServiceIdentifiers } from './container/ServiceIdentifiers'; +import { Chance } from 'chance'; + import { TInputOptions } from './types/options/TInputOptions'; import { IInversifyContainerFacade } from './interfaces/container/IInversifyContainerFacade'; @@ -28,7 +30,7 @@ export class JavaScriptObfuscator { const options: IOptions = inversifyContainerFacade.get(ServiceIdentifiers.IOptions); if (options.seed !== 0) { - RandomGeneratorUtils.setRandomGeneratorSeed(options.seed); + RandomGeneratorUtils.setRandomGenerator(new Chance(options.seed)); } const javaScriptObfuscator: IJavaScriptObfuscator = inversifyContainerFacade diff --git a/src/utils/CryptUtils.ts b/src/utils/CryptUtils.ts index 870cf8af..d2ce86f3 100644 --- a/src/utils/CryptUtils.ts +++ b/src/utils/CryptUtils.ts @@ -58,7 +58,7 @@ export class CryptUtils { return result; }; - const randomString: string = RandomGeneratorUtils.randomGenerator.string({ + const randomString: string = RandomGeneratorUtils.getRandomGenerator().string({ length: length, pool: RandomGeneratorUtils.randomGeneratorPool }); @@ -69,7 +69,7 @@ export class CryptUtils { const randomStringDiffArray: string[] = randomStringDiff.split(''); - RandomGeneratorUtils.randomGenerator.shuffle(randomStringDiffArray); + RandomGeneratorUtils.getRandomGenerator().shuffle(randomStringDiffArray); randomStringDiff = randomStringDiffArray.join(''); return [randomMerge(str, randomStringDiff), randomStringDiff]; diff --git a/src/utils/RandomGeneratorUtils.ts b/src/utils/RandomGeneratorUtils.ts index daac331e..4cf75da2 100644 --- a/src/utils/RandomGeneratorUtils.ts +++ b/src/utils/RandomGeneratorUtils.ts @@ -16,7 +16,7 @@ export class RandomGeneratorUtils { /** * @type {Chance.Chance | Chance.SeededChance} */ - public static randomGenerator: Chance.Chance | Chance.SeededChance = new Chance(); + private static randomGenerator: Chance.Chance | Chance.SeededChance = new Chance(); /** * @param min @@ -24,7 +24,7 @@ export class RandomGeneratorUtils { * @returns {number} */ public static getRandomFloat (min: number, max: number): number { - return RandomGeneratorUtils.randomGenerator.floating({ + return RandomGeneratorUtils.getRandomGenerator().floating({ min: min, max: max, fixed: 7 @@ -50,7 +50,7 @@ export class RandomGeneratorUtils { * @returns {number} */ public static getRandomInteger (min: number, max: number): number { - return RandomGeneratorUtils.randomGenerator.integer({ + return RandomGeneratorUtils.getRandomGenerator().integer({ min: min, max: max }); @@ -83,9 +83,9 @@ export class RandomGeneratorUtils { } /** - * @param randomGeneratorSeed + * @param randomGenerator */ - public static setRandomGeneratorSeed (randomGeneratorSeed: number): void { - RandomGeneratorUtils.randomGenerator = new Chance(randomGeneratorSeed); + public static setRandomGenerator (randomGenerator: Chance.Chance | Chance.SeededChance): void { + RandomGeneratorUtils.randomGenerator = randomGenerator; } } diff --git a/test/functional-tests/JavaScriptObfuscator.spec.ts b/test/functional-tests/JavaScriptObfuscator.spec.ts index f51383f9..67f63184 100644 --- a/test/functional-tests/JavaScriptObfuscator.spec.ts +++ b/test/functional-tests/JavaScriptObfuscator.spec.ts @@ -1,14 +1,21 @@ import { assert } from 'chai'; +import { Chance } from 'chance'; + import { IObfuscationResult } from '../../src/interfaces/IObfuscationResult'; import { JavaScriptObfuscator } from '../../src/JavaScriptObfuscator'; import { NO_CUSTOM_NODES_PRESET } from '../../src/preset-options/NoCustomNodesPreset'; import { readFileAsString } from '../helpers/readFileAsString'; +import { RandomGeneratorUtils } from '../../src/utils/RandomGeneratorUtils'; describe('JavaScriptObfuscator', () => { describe('obfuscate (sourceCode: string, customOptions?: IObfuscatorOptions): IObfuscationResult', () => { + beforeEach(() => { + RandomGeneratorUtils.setRandomGenerator(new Chance()); + }); + describe('if `sourceMap` option is `false`', () => { it('should returns object with obfuscated code and empty source map', () => { let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate( @@ -171,5 +178,9 @@ describe('JavaScriptObfuscator', () => { assert.notEqual(obfuscationResult1.getObfuscatedCode(), obfuscationResult2.getObfuscatedCode()); assert.notEqual(obfuscationResult3.getObfuscatedCode(), obfuscationResult4.getObfuscatedCode()); }); + + afterEach(() => { + RandomGeneratorUtils.setRandomGenerator(new Chance()); + }); }); }); diff --git a/test/functional-tests/node-transformers/node-control-flow-transformers/control-flow-replacers/BinaryExpressionControlFlowReplacer.spec.ts b/test/functional-tests/node-transformers/node-control-flow-transformers/control-flow-replacers/BinaryExpressionControlFlowReplacer.spec.ts index 3617170c..2c9e02a6 100644 --- a/test/functional-tests/node-transformers/node-control-flow-transformers/control-flow-replacers/BinaryExpressionControlFlowReplacer.spec.ts +++ b/test/functional-tests/node-transformers/node-control-flow-transformers/control-flow-replacers/BinaryExpressionControlFlowReplacer.spec.ts @@ -31,7 +31,9 @@ describe('BinaryExpressionControlFlowReplacer', () => { describe('variant #2 - multiple binary expressions with threshold = 1', () => { it('should replace binary expression node by call to control flow storage node', () => { - const samplesCount: number = 50; + const samplesCount: number = 100; + const expectedValue: number = 0.5; + const delta: number = 0.08; let equalsValue: number = 0; @@ -66,7 +68,7 @@ describe('BinaryExpressionControlFlowReplacer', () => { } } - assert.closeTo(equalsValue / samplesCount, 0.5, 0.05); + assert.closeTo(equalsValue / samplesCount, expectedValue, delta); }); }); });