-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathkeygen.ts
58 lines (47 loc) · 1.29 KB
/
keygen.ts
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
const lowercase = 'abcdefghijklmnopqrstuvwxyz';
const uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numbers = '0123456789';
const all = lowercase + uppercase + numbers;
/**
* A utility function that generates a strong password
* @returns
*/
export default function generatePassword(): string {
let password = '';
password += pick(password, lowercase, 1, 5);
password += pick(password, uppercase, 1, 5);
password += pick(password, all, 10);
return shuffle(password);
}
function pick(exclusions: string, string: string, min: number, max?: number): string {
var n: number,
chars = '';
if (max === undefined) {
n = min;
} else {
n = min + Math.floor(Math.random() * (max - min + 1));
}
var i = 0;
while (i < n) {
const character = string.charAt(Math.floor(Math.random() * string.length));
if (exclusions.indexOf(character) < 0 && chars.indexOf(character) < 0) {
chars += character;
i++;
}
}
return chars;
}
function shuffle(string: string): string {
var array = string.split('');
var tmp: string,
current: number,
top = array.length;
if (top)
while (--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array.join('');
}