-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
141 lines (115 loc) · 6.28 KB
/
script.js
File metadata and controls
141 lines (115 loc) · 6.28 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
document.getElementById('generate-password').addEventListener('click', generatePassword);
document.getElementById('generate-recommended-password').addEventListener('click', generateRecommendedPassword);
document.getElementById('copy-password').addEventListener('click', copyToClipboard);
document.getElementById('increase-lengthBy1').addEventListener('click', increasePasswordLengthBy1);
document.getElementById('decrease-lengthBy1').addEventListener('click', decreasePasswordLengthBy1);
document.getElementById('increase-lengthBy5').addEventListener('click', increasePasswordLengthBy5);
document.getElementById('decrease-lengthBy5').addEventListener('click', decreasePasswordLengthBy5);
document.getElementById('resetPasswordLength').addEventListener('click', resetPasswordLength);
const passwordDisplay = document.getElementById('password-display');
const passwordStrength = document.getElementById('password-strength');
function generatePassword() {
const length = parseInt(document.getElementById('password-length').value);
const includeUppercase = document.getElementById('include-uppercase').checked;
const includeLowercase = document.getElementById('include-lowercase').checked;
const includeNumbers = document.getElementById('include-numbers').checked;
const includeSymbols = document.getElementById('include-symbols').checked;
const includeSpecialCharacters = document.getElementById('include-special-characters').checked;
const uppercaseChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const lowercaseChars = 'abcdefghijklmnopqrstuvwxyz';
const numberChars = '0123456789';
const repeatedNumberChars = numberChars.repeat(2); // Repeat numbers to increase their representation
const commonSymbols = '!@#$%^&*()-_=+?'; // Common, more widely accepted symbols
const specialCharacters = '{}[]:;,"\'<>./|\\'; // Special/ambiguous characters
let characters = '';
if (includeUppercase) characters += uppercaseChars;
if (includeLowercase) characters += lowercaseChars;
if (includeNumbers) characters += repeatedNumberChars; // Use repeatedNumberChars instead of numberChars
if (includeSymbols) characters += commonSymbols; // Add common symbols
if (includeSpecialCharacters) characters += specialCharacters; // Add special/ambiguous characters
let password = '';
for (let i = 0; i < length; i++) {
const character = characters[Math.floor(Math.random() * characters.length)];
password += character;
}
document.getElementById("password-code").textContent = password;
assessPasswordStrength(password, length);
}
function generateRecommendedPassword() {
// Set password length to 30
document.getElementById('password-length').value = '30';
// Uncheck "Include Special Characters"
document.getElementById('include-special-characters').checked = false;
// Call the existing generatePassword function
generatePassword();
}
function copyToClipboard() {
const passwordText = document.getElementById("password-code").textContent;
if (!passwordText) return;
navigator.clipboard.writeText(passwordText)
.then(() => alert('Password copied to clipboard!'))
.catch(err => console.error('Error copying password: ', err));
}
function assessPasswordStrength(password, selectedLength) {
let strength = 0;
// Length-based strength
if (selectedLength >= 256) strength += 50; // Adds significant weight for length >= 256
else if (selectedLength >= 25) strength += 5; // For length 25 to 128 (Very Strong)
else if (selectedLength >= 15) strength += 4; // For length 15 (Upper Moderate) to 30 (Strong)
else if (selectedLength >= 12) strength += 2; // For length 12 to 16 (Moderate)
else if (selectedLength >= 8) strength += 1; // For length up to 12 (Weak)
// Complexity-based strength checks
if (/[A-Z]/.test(password)) strength += 2; // Uppercase characters
if (/[a-z]/.test(password)) strength += 2; // Lowercase characters
if (/[0-9]/.test(password)) strength += 2; // Numbers
if (/[^A-Za-z0-9]/.test(password)) strength += 3; // Symbols
// Additional complexity checks
if (/[^A-Za-z0-9]{2,}/.test(password)) strength += 2; // Consecutive non-alphanumeric characters
if (/\d{3,}/.test(password)) strength += 2; // Three or more consecutive digits
if (/[a-zA-Z]{4,}/.test(password)) strength += 2; // Four or more consecutive letters
// Update strength label and class
let strengthLabel = 'Weak🟥';
let strengthClass = 'weak';
if (strength >= 10 && strength < 12) {
strengthLabel = 'Moderate🟧';
strengthClass = 'moderate';
} else if (strength >= 12 && strength < 14) {
strengthLabel = 'Strong🟩';
strengthClass = 'strong';
} else if (strength >= 14 && strength < 25) {
strengthLabel = 'Very Strong🟦';
strengthClass = 'very-strong';
} else if (strength >= 25) {
strengthLabel = 'Incredible💜';
strengthClass = 'incredible';
}
passwordStrength.textContent = `Strength: ${strengthLabel}`;
passwordStrength.className = strengthClass; // Update the class
}
function increasePasswordLengthBy1() {
const passwordLengthSelect = document.getElementById('password-length');
if (passwordLengthSelect.selectedIndex < passwordLengthSelect.options.length - 1) {
passwordLengthSelect.selectedIndex = passwordLengthSelect.selectedIndex + 1;
}
}
function decreasePasswordLengthBy1() {
const passwordLengthSelect = document.getElementById('password-length');
if (passwordLengthSelect.selectedIndex > 0) {
passwordLengthSelect.selectedIndex = passwordLengthSelect.selectedIndex - 1;
}
}
function increasePasswordLengthBy5() {
const passwordLengthSelect = document.getElementById('password-length');
if (passwordLengthSelect.selectedIndex < passwordLengthSelect.options.length - 5) {
passwordLengthSelect.selectedIndex = passwordLengthSelect.selectedIndex + 5;
}
}
function decreasePasswordLengthBy5() {
const passwordLengthSelect = document.getElementById('password-length');
if (passwordLengthSelect.selectedIndex > 0) {
passwordLengthSelect.selectedIndex = passwordLengthSelect.selectedIndex - 5;
}
}
function resetPasswordLength() {
document.getElementById('password-length').value = '6'; // set password length to 6
}