-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
310 lines (268 loc) · 9.83 KB
/
Copy pathscript.js
File metadata and controls
310 lines (268 loc) · 9.83 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* ===== PassGen — Password Generator Logic ===== */
// Developer: zrnge.com
// Repo: github.com/zrnge/passgen
(function () {
'use strict';
// Character sets
const CHARSETS = {
upper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
lower: 'abcdefghijklmnopqrstuvwxyz',
number: '0123456789',
symbol: '!@#$%^&*()_+-=[]{}|;:,.<>?~'
};
const AMBIGUOUS = 'Il1O0';
// DOM
const passwordText = document.getElementById('passwordText');
const passwordDisplay= document.getElementById('passwordDisplay');
const copyBtn = document.getElementById('copyBtn');
const lengthSlider = document.getElementById('lengthSlider');
const lengthValue = document.getElementById('lengthValue');
const upperEl = document.getElementById('uppercase');
const lowerEl = document.getElementById('lowercase');
const numberEl = document.getElementById('numbers');
const symbolEl = document.getElementById('symbols');
const excludeAmbEl = document.getElementById('excludeAmbiguous');
const generateBtn = document.getElementById('generateBtn');
const strengthFill = document.getElementById('strengthFill');
const strengthLabel = document.getElementById('strengthLabel');
const toast = document.getElementById('toast');
let currentPassword = '';
// ---- Secure random integer using crypto ----
function secureRandomInt(max) {
if (max <= 0) return 0;
const array = new Uint32Array(1);
// Rejection sampling for uniform distribution
const limit = 4294967296 - (4294967296 % max);
let val;
do {
crypto.getRandomValues(array);
val = array[0];
} while (val >= limit);
return val % max;
}
// ---- Build the character pool ----
function buildPool() {
let pool = '';
const sets = [];
if (upperEl.checked) sets.push(CHARSETS.upper);
if (lowerEl.checked) sets.push(CHARSETS.lower);
if (numberEl.checked) sets.push(CHARSETS.number);
if (symbolEl.checked) sets.push(CHARSETS.symbol);
if (sets.length === 0) return { pool: '', sets: [] };
// Filter ambiguous characters if requested
if (excludeAmbEl.checked) {
for (let i = 0; i < sets.length; i++) {
sets[i] = sets[i].split('').filter(c => !AMBIGUOUS.includes(c)).join('');
}
}
pool = sets.join('');
return { pool, sets };
}
// ---- Generate password ----
function generatePassword() {
const length = parseInt(lengthSlider.value, 10);
const { pool, sets } = buildPool();
if (!pool) {
passwordText.textContent = 'Select at least one option';
passwordText.classList.add('empty');
currentPassword = '';
updateStrength('');
return;
}
const chars = [];
// Guarantee at least one char from each selected set
for (let i = 0; i < sets.length && i < length; i++) {
chars.push(sets[i][secureRandomInt(sets[i].length)]);
}
// Fill the rest from the full pool
while (chars.length < length) {
chars.push(pool[secureRandomInt(pool.length)]);
}
// Shuffle (Fisher-Yates with secure random)
for (let i = chars.length - 1; i > 0; i--) {
const j = secureRandomInt(i + 1);
[chars[i], chars[j]] = [chars[j], chars[i]];
}
currentPassword = chars.join('');
passwordText.textContent = currentPassword;
passwordText.classList.remove('empty');
updateStrength(currentPassword);
}
// ---- Strength estimation ----
function updateStrength(pw) {
if (!pw) {
strengthFill.style.width = '0%';
strengthFill.style.background = 'var(--danger)';
strengthLabel.textContent = '—';
strengthLabel.style.color = 'var(--text-dim)';
return;
}
let poolSize = 0;
if (/[a-z]/.test(pw)) poolSize += 26;
if (/[A-Z]/.test(pw)) poolSize += 26;
if (/[0-9]/.test(pw)) poolSize += 10;
if (/[^a-zA-Z0-9]/.test(pw)) poolSize += 26;
const entropy = pw.length * Math.log2(poolSize || 1);
const pct = Math.min(100, (entropy / 128) * 100);
let label, color;
if (entropy < 40) { label = 'Weak'; color = 'var(--danger)'; }
else if (entropy < 60) { label = 'Fair'; color = 'var(--warn)'; }
else if (entropy < 80) { label = 'Strong'; color = '#4dffaa'; }
else { label = 'Very Strong'; color = 'var(--ok)'; }
strengthFill.style.width = pct + '%';
strengthFill.style.background = color;
strengthLabel.textContent = label;
strengthLabel.style.color = color;
}
// ---- Copy to clipboard ----
function copyPassword() {
if (!currentPassword) return;
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(currentPassword).then(() => {
copyBtn.classList.add('copied');
showToast('Copied to clipboard!');
setTimeout(() => copyBtn.classList.remove('copied'), 1500);
}).catch(fallbackCopy);
} else {
fallbackCopy();
}
}
// Legacy fallback for non-secure contexts (file://, HTTP)
function fallbackCopy() {
const ta = document.createElement('textarea');
ta.value = currentPassword;
ta.setAttribute('readonly', '');
ta.style.position = 'absolute';
ta.style.left = '-9999px';
document.body.appendChild(ta);
ta.select();
try {
document.execCommand('copy');
copyBtn.classList.add('copied');
showToast('Copied to clipboard!');
setTimeout(() => copyBtn.classList.remove('copied'), 1500);
} catch (e) {
showToast('Copy failed');
}
document.body.removeChild(ta);
}
// ---- Toast ----
let toastTimer;
function showToast(msg) {
toast.textContent = msg;
toast.classList.add('show');
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove('show'), 2000);
}
// ---- Events ----
let sliderTimer;
lengthSlider.addEventListener('input', () => {
lengthValue.textContent = lengthSlider.value;
// Regenerate in real-time, debounced so dragging stays smooth
clearTimeout(sliderTimer);
sliderTimer = setTimeout(generatePassword, 120);
});
generateBtn.addEventListener('click', generatePassword);
copyBtn.addEventListener('click', (e) => { e.stopPropagation(); copyPassword(); });
passwordDisplay.addEventListener('click', copyPassword);
passwordDisplay.addEventListener('keydown', (e) => {
if (e.code === 'Space' || e.code === 'Enter') {
e.preventDefault();
e.stopPropagation();
copyPassword();
}
});
// Regenerate when options change (if a password already exists)
[upperEl, lowerEl, numberEl, symbolEl, excludeAmbEl].forEach(el => {
el.addEventListener('change', () => {
if (currentPassword) generatePassword();
});
});
// Keyboard shortcut: Space / Enter to generate
document.addEventListener('keydown', (e) => {
if (e.code === 'Space' && e.target === document.body) {
e.preventDefault();
generatePassword();
}
});
// ---- Mouse-following glow ----
const mouseGlow = document.getElementById('mouseGlow');
const orbsContainer = document.querySelector('.bg-orbs');
let mouseX = window.innerWidth / 2;
let mouseY = window.innerHeight / 2;
let glowX = mouseX;
let glowY = mouseY;
let glowActive = false;
let parallaxX = 0;
let parallaxY = 0;
let rafId = null;
// Start the animation loop on demand (stops when converged to save CPU)
function startGlowLoop() {
if (rafId) return;
rafId = requestAnimationFrame(animateGlow);
}
document.addEventListener('mousemove', (e) => {
mouseX = e.clientX;
mouseY = e.clientY;
if (!glowActive) {
glowActive = true;
mouseGlow.classList.add('active');
}
startGlowLoop();
});
// Use documentElement — mouseleave on document doesn't fire reliably
document.documentElement.addEventListener('mouseleave', () => {
glowActive = false;
mouseGlow.classList.remove('active');
});
// Smooth animation loop — lerp the glow toward the cursor
// and shift the orb container slightly for a parallax effect.
// Stops itself when converged to avoid running 60 fps forever.
function animateGlow() {
glowX += (mouseX - glowX) * 0.12;
glowY += (mouseY - glowY) * 0.12;
mouseGlow.style.transform = 'translate(' + glowX + 'px, ' + glowY + 'px)';
// Subtle parallax on the orb container (opposite direction for depth).
// Applied to the container because CSS animations on individual orbs
// override inline transforms, so per-orb JS transforms were ignored.
const targetPX = -(mouseX - window.innerWidth / 2) / window.innerWidth * 25;
const targetPY = -(mouseY - window.innerHeight / 2) / window.innerHeight * 25;
parallaxX += (targetPX - parallaxX) * 0.12;
parallaxY += (targetPY - parallaxY) * 0.12;
if (orbsContainer) {
orbsContainer.style.transform = 'translate(' + parallaxX + 'px, ' + parallaxY + 'px)';
}
// Stop the loop when everything has converged
if (Math.abs(mouseX - glowX) < 0.5 &&
Math.abs(mouseY - glowY) < 0.5 &&
Math.abs(targetPX - parallaxX) < 0.5 &&
Math.abs(targetPY - parallaxY) < 0.5) {
rafId = null;
return;
}
rafId = requestAnimationFrame(animateGlow);
}
startGlowLoop();
// ---- Theme toggle (dark / light) ----
const themeToggle = document.getElementById('themeToggle');
function applyTheme(theme) {
if (theme === 'light') {
document.body.classList.add('light');
} else {
document.body.classList.remove('light');
}
}
// Restore saved preference (defaults to dark)
try {
const saved = localStorage.getItem('passgen-theme');
applyTheme(saved === 'light' ? 'light' : 'dark');
} catch (e) {
applyTheme('dark');
}
themeToggle.addEventListener('click', () => {
const isLight = document.body.classList.toggle('light');
try { localStorage.setItem('passgen-theme', isLight ? 'light' : 'dark'); } catch (e) {}
});
// ---- Initial generation ----
generatePassword();
})();