-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
290 lines (236 loc) · 9.43 KB
/
script.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
document.getElementById('generateBtn').addEventListener('click', function () {
try {
const generatedPassword = generatePassword();
updatePasswordStrengthIndicator(generatedPassword);
} catch (error) {
alert(error.message);
}
});
document.getElementById('passwordLength').addEventListener('input', function () {
const passwordLength = this.value;
document.getElementById('passwordLengthOutput').textContent = passwordLength;
});
document.getElementById('quantity').addEventListener('input', function () {
const quantity = this.value;
document.getElementById('quantityOutput').textContent = quantity;
const copyToClipboardBtn = document.getElementById('copyToClipboardBtn');
copyToClipboardBtn.disabled = quantity > 1;
});
document.getElementById('copyToClipboardBtn').addEventListener('click', function () {
try {
const generatedPassword = document.getElementById('generatedPassword').value.trim();
if (generatedPassword === '') {
throw new Error('No password to copy.');
}
navigator.clipboard.writeText(generatedPassword);
Toastify({
text: 'Password copied to clipboard!',
duration: 3000,
backgroundColor: 'linear-gradient(to right, #00b09b, #96c93d)',
close: true,
gravity: 'bottom',
position: 'left',
}).showToast();
} catch (error) {
alert(error.message);
}
});
function generatePassword() {
const passwordLength = document.getElementById('passwordLength').value;
const includeNumbers = document.getElementById('includeNumbers').checked;
const includeLowercase = document.getElementById('includeLowercase').checked;
const includeUppercase = document.getElementById('includeUppercase').checked;
const beginWithLetter = document.getElementById('beginWithLetter').checked;
const includeSymbols = document.getElementById('includeSymbols').checked;
const noSimilarCharacters = document.getElementById('noSimilarCharacters').checked;
const noDuplicateCharacters = document.getElementById('noDuplicateCharacters').checked;
const noSequentialCharacters = document.getElementById('noSequentialCharacters').checked;
const quantity = document.getElementById('quantity').value;
if (!(includeNumbers || includeLowercase || includeUppercase || includeSymbols)) {
throw new Error('Please select at least one character option.');
}
if (isNaN(passwordLength) || passwordLength < 4 || passwordLength > 128) {
throw new Error('Please enter a valid password length between 4 and 128.');
}
let characters = '';
if (includeNumbers) characters += '0123456789';
if (includeLowercase) characters += 'abcdefghijklmnopqrstuvwxyz';
if (includeUppercase) characters += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (includeSymbols) characters += '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';
let generatedPassword = '';
for (let i = 0; i < quantity; i++) {
generatedPassword += generateSinglePassword(characters, passwordLength, beginWithLetter, noDuplicateCharacters, noSequentialCharacters);
if (i < quantity - 1) {
generatedPassword += '\n';
}
}
if (noSimilarCharacters) {
characters = characters.replace(/[il1Lo0O]/g, '');
}
const textarea = document.getElementById('generatedPassword');
textarea.value = generatedPassword;
textarea.rows = quantity;
Toastify({
text: "Passwords generated successfully!",
duration: 3000,
backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)",
close: true,
gravity: "bottom",
position: "left",
}).showToast();
updatePasswordStrengthIndicator(generatedPassword);
return generatedPassword;
}
function generateSinglePassword(characters, length, beginWithLetter, noDuplicateCharacters, noSequentialCharacters) {
let password = '';
if (beginWithLetter) {
const letterCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const randomLetterIndex = Math.floor(Math.random() * letterCharacters.length);
password += letterCharacters.charAt(randomLetterIndex);
length--;
}
if (noDuplicateCharacters) {
characters = removeDuplicateCharacters(characters);
}
if (noSequentialCharacters) {
characters = removeSequentialCharacters(characters);
}
if (length <= 0) {
throw new Error('Password length is too short.');
}
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
password += characters.charAt(randomIndex);
}
return password;
}
function removeDuplicateCharacters(input) {
return input.split('').filter((char, index, self) => {
return index === self.indexOf(char);
}).join('');
}
function removeSequentialCharacters(input) {
return input.replace(/(.)\1+/g, '$1');
}
document.getElementById('checkStrengthBtn').addEventListener('click', function () {
try {
const password = document.getElementById('passwordInput').value.trim();
if (password === '') {
displayEmptyPasswordToast();
return;
}
const passwordStrength = checkPasswordStrength(password);
displayPasswordStrengthToast(passwordStrength);
} catch (error) {
const errorMessageElement = document.getElementById('errorMessage');
if (errorMessageElement) {
errorMessageElement.textContent = error.message;
errorMessageElement.style.display = 'block';
} else {
alert(error.message);
}
}
});
const checkboxes = document.querySelectorAll('.form-check-input');
checkboxes.forEach(checkbox => {
checkbox.addEventListener('change', function () {
updatePasswordStrength();
});
});
document.getElementById('generatedPassword').addEventListener('input', function () {
updatePasswordStrength();
});
function displayEmptyPasswordToast() {
Toastify({
text: 'Please enter a password.',
duration: 3000,
backgroundColor: 'linear-gradient(to right, #ff8a00, #da1b60)',
close: true,
gravity: 'bottom',
position: 'left',
}).showToast();
}
function checkPasswordStrength(password) {
const hasUppercase = /[A-Z]/.test(password);
const hasLowercase = /[a-z]/.test(password);
const hasNumber = /\d/.test(password);
const hasSpecialChar = /[!@#$%^&*()_+{}\[\]:;<>,.?~\\-]/.test(password);
const typesCount = [hasUppercase, hasLowercase, hasNumber, hasSpecialChar].filter(Boolean).length;
if (password.length < 8 || typesCount < 3) {
return 'Weak';
} else if (password.length < 12 || typesCount === 3) {
return 'Moderate';
} else {
return 'Strong';
}
}
function displayPasswordStrengthToast(strength) {
let message;
switch (strength) {
case 'Weak' || 'Very Weak':
message = 'Weak password! Please consider a stronger one.';
break;
case 'Moderate':
message = 'Medium strength password.';
break;
case 'Strong' || 'Very Strong':
message = 'Strong password! Good job!';
break;
default:
message = 'Invalid password strength';
}
Toastify({
text: message,
duration: 3000,
backgroundColor: 'linear-gradient(to right, #ff8a00, #da1b60)',
close: true,
gravity: 'bottom',
position: 'left',
}).showToast();
}
function updatePasswordStrength() {
const generatedPassword = document.getElementById('generatedPassword').value.trim();
updatePasswordStrengthIndicator(generatedPassword);
}
function updatePasswordStrengthIndicator(password) {
if (password === undefined || password.trim() === '') {
console.warn('Password is undefined or empty. Skipping password strength update.');
return;
}
const passwordStrengthBar = document.getElementById('passwordStrengthBar');
const strength = calculatePasswordStrength(password);
passwordStrengthBar.style.width = `${strength * 7.5}%`;
passwordStrengthBar.classList.remove('bg-danger', 'bg-warning', 'bg-info', 'bg-success');
if (strength >= 14) {
passwordStrengthBar.classList.add('bg-success');
} else if (strength >= 12) {
passwordStrengthBar.classList.add('bg-info');
} else if (strength >= 10) {
passwordStrengthBar.classList.add('bg-warning');
} else {
passwordStrengthBar.classList.add('bg-danger');
}
}
function calculatePasswordStrength(password) {
let strength = 0;
const hasLowercase = /[a-z]/.test(password);
const hasUppercase = /[A-Z]/.test(password);
const hasDigit = /\d/.test(password);
const hasSpecialChar = /[!@#$%^&*(),.?":{}|<>]/.test(password);
const typesCount = [hasLowercase, hasUppercase, hasDigit, hasSpecialChar].filter(Boolean).length;
strength += typesCount * 2;
const hasCommonPatterns = /(123|abc|qwerty|password)/i.test(password);
const hasSequentialChars = /(012|123|234|345|456|567|678|789|890|abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz)/i.test(password);
if (!hasCommonPatterns) {
strength += 2;
}
if (!hasSequentialChars) {
strength += 2;
}
if (password.length >= 14) {
strength += 2;
} else if (password.length >= 10) {
strength += 1;
}
return strength;
}