-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (94 loc) · 2.89 KB
/
index.js
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
119
120
121
const csprng = require ( 'csprng-num' ) ;
const letters = "abcdefghijklmnopqrstuvwxyz";
const capitalletters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numbers = "0123456789";
const specialcharacters = "!@#$%^&*()_+~`|}{[]\:;?><,./-=";
const allchar = letters + capitalletters + numbers + specialcharacters;
var passwordList = [];
function CheckUnique(password){
if(passwordList.includes(password)){
return false;
}else{
passwordList.push(password);
return true;
}
}
function SetPasswordList(list){
passwordList = list;
return true
}
function GenerateWithControl({lettercount, capitalcount, numbercount, specialcount}){
var chars = "";
var password = "";
for(var i = 0; i < lettercount; i++){
chars += letters[Math.floor(csprng() * letters.length)];
}
for(var i = 0; i < capitalcount; i++){
chars += capitalletters[Math.floor(csprng() * capitalletters.length)];
}
for(var i = 0; i < numbercount; i++){
chars += numbers[Math.floor(csprng() * numbers.length)];
}
for(var i = 0; i < specialcount; i++){
chars += specialcharacters[Math.floor(csprng() * specialcharacters.length)];
}
//shuffle the characters from chars
chars = chars.split("");
for(var i = chars.length - 1; i > 0; i--){
var j = Math.floor(csprng() * (i + 1));
var temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
password = chars.join("");
if(CheckUnique(password)){
return password;
}
else {
return GenerateWithControl({lettercount, capitalcount, numbercount, specialcount});
}
}
function GenerateRandomPassword(length){
var password = "";
for(var i = 0; i < length; i++){
password += allchar[Math.floor(csprng() * allchar.length)];
}
password = password.split("");
for(var i = password.length - 1; i > 0; i--){
var j = Math.floor(csprng() * (i + 1));
var temp = password[i];
password[i] = password[j];
password[j] = temp;
}
password = password.join("");
if(CheckUnique(password)){
return password;
}
else {
return GenerateRandomPassword(length);
}
}
function GenPassSetChars({length, chars}){
var password = "";
for(var i = 0; i < length; i++){
password += chars[Math.floor(csprng() * chars.length)];
}
password = password.split("");
for(var i = password.length - 1; i > 0; i--){
var j = Math.floor(csprng() * (i + 1));
var temp = password[i];
password[i] = password[j];
password[j] = temp;
}
password = password.join("");
if(CheckUnique(password)){
return password;
}
else {
GenPassSetChars({length, chars});
}
}
function GetPass(){
return passwordList;
}
module.exports = {GenerateWithControl, GenerateRandomPassword, GenPassSetChars, SetPasswordList, GetPass};