-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
186 lines (157 loc) · 6.05 KB
/
Copy pathscript.js
File metadata and controls
186 lines (157 loc) · 6.05 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
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
const passwordOutput = document.getElementById('password-output');
const copyBtn = document.getElementById('copy-btn');
const strengthBar = document.getElementById('strength-bar');
const strengthText = document.getElementById('strength-text');
const lengthSlider = document.getElementById('length-slider');
const lengthVal = document.getElementById('length-val');
const uppercaseToggle = document.getElementById('uppercase');
const lowercaseToggle = document.getElementById('lowercase');
const numbersToggle = document.getElementById('numbers');
const symbolsToggle = document.getElementById('symbols');
const excludeAmbiguousToggle = document.getElementById('exclude-ambiguous');
const generateBtn = document.getElementById('generate-btn');
// Character sets
const CHARS = {
uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
lowercase: 'abcdefghijklmnopqrstuvwxyz',
numbers: '0123456789',
symbols: '!@#$%^&*()_+~`|}{[]:;?><,./-=',
ambiguous: 'il1Oo0I'
};
// Sync length slider value
lengthSlider.addEventListener('input', (e) => {
lengthVal.textContent = e.target.value;
});
// Secure Random Generation
function secureRandomIndex(max) {
const array = new Uint32Array(1);
window.crypto.getRandomValues(array);
return array[0] % max;
}
function calculateEntropy(length, poolSize) {
if (poolSize === 0) return 0;
return length * Math.log2(poolSize);
}
function updateStrengthMeter(entropy) {
let strength = 0;
let text = 'Very Weak';
let colorClass = '';
if (entropy <= 0) {
strength = 0;
text = 'None';
} else if (entropy < 40) {
strength = 1;
text = 'Weak';
} else if (entropy < 60) {
strength = 2;
text = 'Fair';
} else if (entropy < 80) {
strength = 3;
text = 'Good';
} else {
strength = 4;
text = 'Strong';
}
strengthBar.setAttribute('data-strength', strength);
strengthText.textContent = text;
// Change text color based on strength
const colors = ['var(--text-secondary)', 'var(--danger)', 'var(--warning)', '#a371f7', 'var(--success)'];
strengthText.style.color = colors[strength];
}
function generatePassword() {
let pool = '';
const length = parseInt(lengthSlider.value, 10);
const useUpper = uppercaseToggle.checked;
const useLower = lowercaseToggle.checked;
const useNums = numbersToggle.checked;
const useSyms = symbolsToggle.checked;
const excludeAmbiguous = excludeAmbiguousToggle.checked;
if (useUpper) pool += CHARS.uppercase;
if (useLower) pool += CHARS.lowercase;
if (useNums) pool += CHARS.numbers;
if (useSyms) pool += CHARS.symbols;
if (excludeAmbiguous) {
const ambiguousRegex = new RegExp(`[${CHARS.ambiguous}]`, 'g');
pool = pool.replace(ambiguousRegex, '');
}
if (pool.length === 0) {
passwordOutput.value = '';
updateStrengthMeter(0);
return;
}
let password = '';
// Ensure at least one character from each selected pool if length permits
const guaranteedChars = [];
if (useUpper) {
let uPool = CHARS.uppercase;
if (excludeAmbiguous) uPool = uPool.replace(new RegExp(`[${CHARS.ambiguous}]`, 'g'), '');
if (uPool.length > 0) guaranteedChars.push(uPool[secureRandomIndex(uPool.length)]);
}
if (useLower) {
let lPool = CHARS.lowercase;
if (excludeAmbiguous) lPool = lPool.replace(new RegExp(`[${CHARS.ambiguous}]`, 'g'), '');
if (lPool.length > 0) guaranteedChars.push(lPool[secureRandomIndex(lPool.length)]);
}
if (useNums) {
let nPool = CHARS.numbers;
if (excludeAmbiguous) nPool = nPool.replace(new RegExp(`[${CHARS.ambiguous}]`, 'g'), '');
if (nPool.length > 0) guaranteedChars.push(nPool[secureRandomIndex(nPool.length)]);
}
if (useSyms) {
let sPool = CHARS.symbols;
// usually symbols don't have ambiguous chars, but just in case
if (sPool.length > 0) guaranteedChars.push(sPool[secureRandomIndex(sPool.length)]);
}
// Fill the rest
for (let i = guaranteedChars.length; i < length; i++) {
password += pool[secureRandomIndex(pool.length)];
}
// Shuffle the result including guaranteed characters
const passwordArray = [...guaranteedChars, ...password.split('')];
for (let i = passwordArray.length - 1; i > 0; i--) {
const j = secureRandomIndex(i + 1);
[passwordArray[i], passwordArray[j]] = [passwordArray[j], passwordArray[i]];
}
const finalPassword = passwordArray.join('');
// Truncate if guaranteed chars pushed it over length, which shouldn't happen for length > 4
passwordOutput.value = finalPassword.slice(0, length);
// Calculate strength based on entropy
const entropy = calculateEntropy(length, pool.length);
updateStrengthMeter(entropy);
}
// Copy to Clipboard
async function copyToClipboard() {
const password = passwordOutput.value;
if (!password || password === "Click 'Generate'") return;
try {
await navigator.clipboard.writeText(password);
// UI feedback
copyBtn.innerHTML = '<i class="fa-solid fa-check"></i>';
copyBtn.classList.add('copied');
setTimeout(() => {
copyBtn.innerHTML = '<i class="fa-regular fa-copy"></i>';
copyBtn.classList.remove('copied');
}, 2000);
} catch (err) {
console.error('Failed to copy text: ', err);
// Fallback for older browsers
passwordOutput.select();
document.execCommand('copy');
}
}
// Event Listeners
generateBtn.addEventListener('click', generatePassword);
copyBtn.addEventListener('click', copyToClipboard);
// Auto-generate on toggle change
const toggles = [
lengthSlider, uppercaseToggle, lowercaseToggle,
numbersToggle, symbolsToggle, excludeAmbiguousToggle
];
toggles.forEach(toggle => {
toggle.addEventListener('change', () => {
// Only auto-generate if a password has already been generated once or is empty
if (passwordOutput.value !== "Click 'Generate'") {
generatePassword();
}
});
});