-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (117 loc) · 6.27 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
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
const PasswordGeneratorModule = (function() {
function getCharset(includeLowercase, includeUppercase, includeNumbers, includeSpecialChars, excludeAmbiguous) {
const lowercaseCharset = excludeAmbiguous ? "abcdefghijkmnpqrstuvwxyz" : "abcdefghijklmnopqrstuvwxyz"; // Excluding "l" and "I" if checked
const uppercaseCharset = excludeAmbiguous ? "ABCDEFGHJKLMNPQRSTUVWXYZ" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // Excluding "I" and "O" if checked
const numberCharset = excludeAmbiguous ? "23456789" : "0123456789"; // Excluding "0" and "1" if checked
const specialCharset = "!@#$%^&*";
let charset = "";
if (includeLowercase) charset += lowercaseCharset;
if (includeUppercase) charset += uppercaseCharset;
if (includeNumbers) charset += numberCharset;
if (includeSpecialChars) charset += specialCharset;
return charset;
}
// Function to ensure minimum numbers and special characters
function addMinimumChars(password, minNumbers, minSpecialChars, numberCharset, specialCharset) {
let numCount = 0;
let specialCount = 0;
// Add minimum required numbers
while (numCount < minNumbers) {
password += numberCharset[Math.floor(Math.random() * numberCharset.length)];
numCount++;
}
// Add minimum required special characters
while (specialCount < minSpecialChars) {
password += specialCharset[Math.floor(Math.random() * specialCharset.length)];
specialCount++;
}
return password;
}
function validateInputs(length, minNumbers, minSpecialChars, includeLowercase, includeUppercase, includeNumbers, includeSpecialChars) {
// Check if at least one character type is selected
if (!includeLowercase && !includeUppercase && !includeNumbers && !includeSpecialChars) {
alert("Please select at least one character type (lowercase, uppercase, numbers, or special characters).");
return false;
}
// Ensure the sum of minNumbers and minSpecialChars doesn't exceed password length
if (minNumbers + minSpecialChars > length) {
alert("The sum of minimum numbers and special characters cannot exceed the password length.");
return false;
}
// Ensure at least one uppercase & one lowercase letter exists in password if both checkboxes are selected
if ((includeLowercase && includeUppercase) && minNumbers + minSpecialChars === length-1) {
alert("At least one uppercase & lowercase letter must be included if both uppercase & lowercase checkboxes are selected. You may need to decrease the minimum number & special character values to accomplish this.");
return false;
}
// Ensure at least one lowercase letter if the lowercase checkbox is selected
if (includeLowercase && minNumbers + minSpecialChars === length) {
alert("At least one lowercase letter must be included if the lowercase checkbox is selected. You may need to decrease the minimum number & special character values to accomplish this.");
return false;
}
// Ensure at least one uppercase letter if the uppercase checkbox is selected
if (includeUppercase && minNumbers + minSpecialChars === length) {
alert("At least one uppercase letter must be included if the uppercase checkbox is selected. You may need to decrease the minimum number & special character values to accomplish this.");
return false;
}
return true;
}
function generatePassword() {
const length = parseInt(document.getElementById('length').value);
let minNumbers = parseInt(document.getElementById('minNumbers').value);
let minSpecialChars = parseInt(document.getElementById('minSpecialChars').value);
const includeLowercase = document.getElementById('includeLowercase').checked;
const includeUppercase = document.getElementById('includeUppercase').checked;
const includeNumbers = document.getElementById('includeNumbers').checked;
const includeSpecialChars = document.getElementById('includeSpecialChars').checked;
const excludeAmbiguous = document.getElementById('excludeAmbiguous').checked;
// Validate inputs
if (!validateInputs(length, minNumbers, minSpecialChars, includeLowercase, includeUppercase, includeNumbers, includeSpecialChars)) {
return; // Exit early if validation fails
}
// If numbers checkbox is unchecked, treat minNumbers as 0
if (!includeNumbers) {
minNumbers = 0;
}
// If special characters checkbox is unchecked, treat minSpecialChars as 0
if (!includeSpecialChars) {
minSpecialChars = 0;
}
const charset = getCharset(includeLowercase, includeUppercase, includeNumbers, includeSpecialChars, excludeAmbiguous);
const numberCharset = excludeAmbiguous ? "23456789" : "0123456789"; // Excluding "0" and "1" if checked
const specialCharset = "!@#$%^&*";
// Ensure password meets minimum numbers and special characters requirement
let password = "";
password = addMinimumChars(password, minNumbers, minSpecialChars, numberCharset, specialCharset);
// Fill the rest of the password randomly from the selected charset
for (let i = password.length; i < length; i++) {
const randomIndex = Math.floor(Math.random() * charset.length);
password += charset[randomIndex];
}
// Ensure at least one lowercase letter if required
if (includeLowercase && !/[a-z]/.test(password)) {
password = password.slice(0, password.length - 1) + lowercaseCharset[Math.floor(Math.random() * lowercaseCharset.length)];
}
// Ensure at least one uppercase letter if required
if (includeUppercase && !/[A-Z]/.test(password)) {
password = password.slice(0, password.length - 1) + uppercaseCharset[Math.floor(Math.random() * uppercaseCharset.length)];
}
// Shuffle password to randomize the order (to prevent having the numbers and special chars always at the beginning)
password = shuffleString(password);
// Display the generated password
document.getElementById('password').textContent = password;
}
// Function to shuffle a string (to mix numbers and special chars into the final password randomly)
function shuffleString(str) {
const arr = str.split('');
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]]; // Swap elements
}
return arr.join('');
}
return {
generatePassword,
};
})();
const passwordGenerationButton = document.querySelector("#generate-password-button");
passwordGenerationButton.addEventListener("click", PasswordGeneratorModule.generatePassword);