forked from javascript-obfuscator/javascript-obfuscator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomGenerator.ts
More file actions
118 lines (100 loc) · 3.16 KB
/
RandomGenerator.ts
File metadata and controls
118 lines (100 loc) · 3.16 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { inject, injectable, postConstruct } from 'inversify';
import { ServiceIdentifiers } from '../container/ServiceIdentifiers';
import md5 from 'md5';
import { Chance } from 'chance';
import { IInitializable } from '../interfaces/IInitializable';
import { IOptions } from '../interfaces/options/IOptions';
import { IRandomGenerator } from '../interfaces/utils/IRandomGenerator';
import { ISourceCode } from '../interfaces/source-code/ISourceCode';
import { initializable } from '../decorators/Initializable';
@injectable()
export class RandomGenerator implements IRandomGenerator, IInitializable {
/**
* @type {string}
*/
public static readonly randomGeneratorPool: string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
/**
* @type {IOptions}
*/
private readonly options: IOptions;
/**
* @type {Chance.Chance}
*/
@initializable()
private randomGenerator!: Chance.Chance;
/**
* @type {number}
*/
@initializable()
private seed!: number;
/**
* @type {ISourceCode}
*/
private readonly sourceCode: ISourceCode;
/**
* @param {ISourceCode} sourceCode
* @param {IOptions} options
*/
constructor (
@inject(ServiceIdentifiers.ISourceCode) sourceCode: ISourceCode,
@inject(ServiceIdentifiers.IOptions) options: IOptions
) {
this.sourceCode = sourceCode;
this.options = options;
}
@postConstruct()
public initialize (): void {
const getRandomInteger: (min: number, max: number) => number = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1) + min);
};
/**
* We need to add numbers from md5 hash of source code to input seed to prevent same String Array name
* for different bundles with same seed
*
* @returns {number}
*/
const getSeed: () => number = (): number => {
const md5Hash: string = md5(this.sourceCode.getSourceCode());
return this.seed + Number(md5Hash.replace(/\D/g, ''));
};
this.seed = this.options.seed !== 0 ? this.options.seed : getRandomInteger(0, 999_999_999);
this.randomGenerator = new Chance(getSeed());
}
/**
* @returns {number}
*/
public getMathRandom (): number {
return this.getRandomInteger(0, 99999) / 100000;
}
/**
* @returns {Chance.Chance}
*/
public getRandomGenerator (): Chance.Chance {
return this.randomGenerator;
}
/**
* @param {number} min
* @param {number} max
* @returns {number}
*/
public getRandomInteger (min: number, max: number): number {
return this.getRandomGenerator().integer({
min: min,
max: max
});
}
/**
* @param {number} length
* @param {string} pool
* @returns {string}
*/
public getRandomString (length: number, pool: string = RandomGenerator.randomGeneratorPool): string {
return this.getRandomGenerator().string({ length, pool });
}
/**
* @returns {number}
*/
public getSeed (): number {
return this.seed;
}
}