forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added number numerical expression analyzer
- Loading branch information
sanex3339
committed
Jul 12, 2020
1 parent
160ff20
commit f07452e
Showing
12 changed files
with
31,950 additions
and
20 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/analyzers/number-numerical-expression-analyzer/NumberNumericalExpressionAnalyzer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { injectable, inject } from 'inversify'; | ||
|
||
import { TNumberNumericalExpressionData } from '../../types/analyzers/number-numerical-expression-analyzer/TNumberNumericalExpressionData'; | ||
|
||
import { INumberNumericalExpressionAnalyzer } from '../../interfaces/analyzers/number-numerical-expression-analyzer/INumberNumericalExpressionAnalyzer'; | ||
import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator'; | ||
|
||
import { ServiceIdentifiers } from '../../container/ServiceIdentifiers'; | ||
|
||
/** | ||
* Rework of https://gist.github.com/da411d/0e59f79dcf4603cdabf0024a10eeb6fe | ||
*/ | ||
@injectable() | ||
export class NumberNumericalExpressionAnalyzer implements INumberNumericalExpressionAnalyzer { | ||
/** | ||
* @type {number} | ||
*/ | ||
private static readonly additionalParts: number = 5; | ||
|
||
/** | ||
* @type {IRandomGenerator} | ||
*/ | ||
private readonly randomGenerator: IRandomGenerator; | ||
|
||
/** | ||
* @param {IRandomGenerator} randomGenerator | ||
*/ | ||
public constructor ( | ||
@inject(ServiceIdentifiers.IRandomGenerator) randomGenerator: IRandomGenerator | ||
) { | ||
this.randomGenerator = randomGenerator; | ||
} | ||
|
||
/** | ||
* @param {number} number | ||
* @returns {TNumberNumericalExpressionData} | ||
*/ | ||
public analyze (number: number): TNumberNumericalExpressionData { | ||
if (isNaN(number)) { | ||
throw new Error('Given value is NaN'); | ||
} | ||
|
||
const additionParts: number[] = this.generateAdditionParts(number); | ||
|
||
return additionParts.map((addition: number) => this.mixWithMultiplyParts(addition)); | ||
} | ||
|
||
/** | ||
* @param {number} number | ||
* @returns {number[]} | ||
*/ | ||
private generateAdditionParts (number: number): number[] { | ||
const additionParts = []; | ||
|
||
const from: number = Math.min(-10000, -Math.abs(number * 2)); | ||
const to: number = Math.max(10000, Math.abs(number * 2)); | ||
|
||
let temporarySum = 0; | ||
|
||
for (let i = 0; i < NumberNumericalExpressionAnalyzer.additionalParts; i++) { | ||
if (i < NumberNumericalExpressionAnalyzer.additionalParts - 1) { | ||
// trailing parts | ||
|
||
const addition: number = this.randomGenerator.getRandomInteger(from, to); | ||
|
||
additionParts.push(addition); | ||
temporarySum += addition; | ||
} else { | ||
// last part | ||
|
||
additionParts.push(number - temporarySum); | ||
} | ||
} | ||
|
||
return additionParts; | ||
} | ||
|
||
/** | ||
* @param {number} number | ||
* @returns {number | number[]} | ||
*/ | ||
private mixWithMultiplyParts (number: number): number | number[] { | ||
const dividers: number[] = this.getDividers(number); | ||
|
||
const shouldMixWithMultiplyParts: boolean = this.randomGenerator.getMathRandom() > 0.5; | ||
|
||
if (!shouldMixWithMultiplyParts || !dividers.length) { | ||
return number; | ||
} | ||
|
||
const divider = dividers[ | ||
this.randomGenerator.getRandomInteger(0, dividers.length - 1) | ||
]; | ||
|
||
return [divider, number / divider]; | ||
} | ||
|
||
/** | ||
* @param {number} number | ||
* @returns {number[]} | ||
*/ | ||
private getDividers (number: number): number[] { | ||
const dividers: number[] = []; | ||
|
||
number = Math.abs(number); | ||
|
||
for (let i = 2; i < number; i++) { | ||
if (number % i === 0){ | ||
dividers.push(i); | ||
} | ||
} | ||
|
||
return dividers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...aces/analyzers/number-numerical-expression-analyzer/INumberNumericalExpressionAnalyzer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { TNumberNumericalExpressionData } from '../../../types/analyzers/number-numerical-expression-analyzer/TNumberNumericalExpressionData'; | ||
|
||
export interface INumberNumericalExpressionAnalyzer { | ||
/** | ||
* @param {number} number | ||
* @returns {TNumberNumericalExpressionData} | ||
*/ | ||
analyze (number: number): TNumberNumericalExpressionData; | ||
} |
1 change: 1 addition & 0 deletions
1
src/types/analyzers/number-numerical-expression-analyzer/TNumberNumericalExpressionData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type TNumberNumericalExpressionData = (number | number[])[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
.../analyzers/number-numerical-expression-analyzer/NumberNumericalExpressionAnalyzer.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import 'reflect-metadata'; | ||
|
||
import { assert } from 'chai'; | ||
|
||
import { ServiceIdentifiers } from '../../../../src/container/ServiceIdentifiers'; | ||
|
||
import { TNumberNumericalExpressionData } from '../../../../src/types/analyzers/number-numerical-expression-analyzer/TNumberNumericalExpressionData'; | ||
|
||
import { IInversifyContainerFacade } from '../../../../src/interfaces/container/IInversifyContainerFacade'; | ||
import { INumberNumericalExpressionAnalyzer } from '../../../../src/interfaces/analyzers/number-numerical-expression-analyzer/INumberNumericalExpressionAnalyzer'; | ||
|
||
import { InversifyContainerFacade } from '../../../../src/container/InversifyContainerFacade'; | ||
|
||
/** | ||
* @param {TNumberNumericalExpressionData} data | ||
* @returns {string} | ||
*/ | ||
const numberNumericalExpressionDataToString = (data: TNumberNumericalExpressionData) => | ||
data | ||
.map((part: number | number[]) => Array.isArray(part) ? part.join('*') : part) | ||
.join('+') | ||
.replace(/\+-/g, '-'); | ||
|
||
describe('NumberNumericalExpressionAnalyzer', () => { | ||
let numberNumericalExpressionAnalyzer: INumberNumericalExpressionAnalyzer; | ||
|
||
before(() => { | ||
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade(); | ||
|
||
inversifyContainerFacade.load('', '', {}); | ||
numberNumericalExpressionAnalyzer = inversifyContainerFacade | ||
.get<INumberNumericalExpressionAnalyzer>(ServiceIdentifiers.INumberNumericalExpressionAnalyzer); | ||
}); | ||
|
||
describe('analyze', () => { | ||
let evaluatedResult: number; | ||
|
||
describe('Variant #1: positive number', () => { | ||
const number: number = 1234; | ||
|
||
before(() => { | ||
const numberNumericalExpressionData: TNumberNumericalExpressionData = | ||
numberNumericalExpressionAnalyzer.analyze(number); | ||
|
||
evaluatedResult = eval(numberNumericalExpressionDataToString(numberNumericalExpressionData)); | ||
}); | ||
|
||
it('should return correct number numerical expression data', () => { | ||
assert.equal(number, evaluatedResult); | ||
}); | ||
}); | ||
|
||
describe('Variant #2: negative number', () => { | ||
const number: number = -1234; | ||
|
||
before(() => { | ||
const numberNumericalExpressionData: TNumberNumericalExpressionData = | ||
numberNumericalExpressionAnalyzer.analyze(number); | ||
|
||
evaluatedResult = eval(numberNumericalExpressionDataToString(numberNumericalExpressionData)); | ||
}); | ||
|
||
it('should return correct number numerical expression data', () => { | ||
assert.equal(number, evaluatedResult); | ||
}); | ||
}); | ||
|
||
describe('Variant #3: zero number', () => { | ||
const number: number = 0; | ||
|
||
before(() => { | ||
const numberNumericalExpressionData: TNumberNumericalExpressionData = | ||
numberNumericalExpressionAnalyzer.analyze(number); | ||
|
||
evaluatedResult = eval(numberNumericalExpressionDataToString(numberNumericalExpressionData)); | ||
}); | ||
|
||
it('should return correct number numerical expression data', () => { | ||
assert.equal(number, evaluatedResult); | ||
}); | ||
}); | ||
|
||
describe('Variant #4: NaN', () => { | ||
const number: number = NaN; | ||
|
||
let testFunc: () => void; | ||
|
||
before(() => { | ||
testFunc = () => numberNumericalExpressionAnalyzer.analyze(number); | ||
}); | ||
|
||
it('should throw error', () => { | ||
assert.throw(testFunc, 'Given value is NaN'); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -369,10 +369,10 @@ | |
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.9.3.tgz#6356df2647de9eac569f9a52eda3480fa9e70b4d" | ||
integrity sha512-01s+ac4qerwd6RHD+mVbOEsraDHSgUaefQlEdBbUolnQFjKwCr7luvAlEwW1RFojh67u0z4OUTjPn9LEl4zIkA== | ||
|
||
"@types/[email protected].20": | ||
version "14.0.20" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.20.tgz#0da05cddbc761e1fa98af88a17244c8c1ff37231" | ||
integrity sha512-MRn/NP3dee8yL5QhbSA6riuwkS+UOcsPUMOIOG3KMUQpuor/2TopdRBu8QaaB4fGU+gz/bzyDWt0FtUbeJ8H1A== | ||
"@types/[email protected].22": | ||
version "14.0.22" | ||
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.22.tgz#23ea4d88189cec7d58f9e6b66f786b215eb61bdc" | ||
integrity sha512-emeGcJvdiZ4Z3ohbmw93E/64jRzUHAItSHt8nF7M4TGgQTiWqFVGB8KNpLGFmUHmHLvjvBgFwVlqNcq+VuGv9g== | ||
|
||
"@types/normalize-package-data@^2.4.0": | ||
version "2.4.0" | ||
|
@@ -1923,10 +1923,10 @@ [email protected]: | |
resolve "^1.17.0" | ||
tsconfig-paths "^3.9.0" | ||
|
||
[email protected].2: | ||
version "29.1.2" | ||
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-29.1.2.tgz#542beba99946cb8d184e5261ebc632b24673f825" | ||
integrity sha512-s1uJcPcjZQ4Z5i98T3zWkp/gzJ9AtHGXXg0zxd0wCnFzX8RQU6awdUokUlBFMoWZJZxdCAXDtEIQBRfr/Lrsjw== | ||
[email protected].3: | ||
version "29.1.3" | ||
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-29.1.3.tgz#288c3faf142fbf78e6abff6a838db23af8d6749f" | ||
integrity sha512-HEB8jPsWBGu++LffL4K8VZ7VXz0HELI1I3Qkv/+5oSekgrAo6I0AVgl5abecLbTQQZo0OaEcmTptiIspwDOu1w== | ||
dependencies: | ||
comment-parser "^0.7.5" | ||
debug "^4.1.1" | ||
|
@@ -5432,10 +5432,10 @@ [email protected]: | |
v8-compile-cache "^2.1.1" | ||
yargs "^13.3.2" | ||
|
||
webpack-node-externals@2.3.0: | ||
version "2.3.0" | ||
resolved "https://registry.yarnpkg.com/webpack-node-externals/-/webpack-node-externals-2.3.0.tgz#c884c07103de080284e17c195fe87c68e2b9c668" | ||
integrity sha512-d1scCn/L5hv73GMOlqSTO6ykLWOiUrZfn54xQYf7u0yGLlUSf5trq6HV/Gw8JIpH2NEyXS7bJec1gk9YR/Qdqw== | ||
webpack-node-externals@2.5.0: | ||
version "2.5.0" | ||
resolved "https://registry.yarnpkg.com/webpack-node-externals/-/webpack-node-externals-2.5.0.tgz#8d50f3289c71bc2b921a8da228e0b652acc503f1" | ||
integrity sha512-g7/Z7Q/gsP8GkJkKZuJggn6RSb5PvxW1YD5vvmRZIxaSxAzkqjfL5n9CslVmNYlSqBVCyiqFgOqVS2IOObCSRg== | ||
|
||
webpack-sources@^1.4.0, webpack-sources@^1.4.1: | ||
version "1.4.3" | ||
|