-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
180 lines (150 loc) · 5.5 KB
/
Copy pathscript.js
File metadata and controls
180 lines (150 loc) · 5.5 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
// Character sets
const SETS = {
upper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
lower: 'abcdefghijklmnopqrstuvwxyz',
numbers: '0123456789',
symbols: '!@#$%^&*()-_=+[]{}|;:,.<>?/`~'
};
// UI elements
const lengthRange = document.getElementById('length');
const lengthVal = document.getElementById('lengthVal');
const passwordEl = document.getElementById('password');
const generateBtn = document.getElementById('generateBtn');
const generateBtn2 = document.getElementById('generateBtn2');
const copyBtn = document.getElementById('copyBtn');
const copyBtn2 = document.getElementById('copyBtn2');
const meterFill = document.getElementById('meterFill');
const strengthText = document.getElementById('strengthText');
const entropyEl = document.getElementById('entropy');
const charsetSizeEl = document.getElementById('charsetSize');
const upper = document.getElementById('upper');
const lower = document.getElementById('lower');
const numbers = document.getElementById('numbers');
const symbols = document.getElementById('symbols');
const noSimilar = document.getElementById('noSimilar');
function updateLengthUI() {
lengthVal.textContent = lengthRange.value;
}
lengthRange.addEventListener('input', () => {
updateLengthUI();
updatePreviewStats();
});
// Secure random integer using Web Crypto API
function secureRandomInt(max) {
const uint32 = crypto.getRandomValues(new Uint32Array(1))[0];
return Math.floor(uint32 / 0x100000000 * max);
}
function buildCharset() {
let chars = '';
if (upper.checked) chars += SETS.upper;
if (lower.checked) chars += SETS.lower;
if (numbers.checked) chars += SETS.numbers;
if (symbols.checked) chars += SETS.symbols;
if (noSimilar.checked) {
chars = chars.replace(/[il1Lo0O]/g, '');
}
return chars;
}
function generatePassword(len) {
const chars = buildCharset();
if (!chars) return '';
const out = [];
// Collect active character sets (for guaranteed inclusion)
const activeTypes = [];
if (upper.checked) activeTypes.push(SETS.upper);
if (lower.checked) activeTypes.push(SETS.lower);
if (numbers.checked) activeTypes.push(SETS.numbers);
if (symbols.checked) activeTypes.push(SETS.symbols);
if (noSimilar.checked) {
for (let i = 0; i < activeTypes.length; i++) {
activeTypes[i] = activeTypes[i].replace(/[il1Lo0O]/g, '');
}
}
// Guarantee at least one character from each selected type (if possible)
const reserve = Math.min(len, activeTypes.length);
for (let i = 0; i < reserve; i++) {
const set = activeTypes[i];
if (set && set.length > 0) {
out.push(set[secureRandomInt(set.length)]);
}
}
// Fill remaining positions
for (let i = out.length; i < len; i++) {
out.push(chars[secureRandomInt(chars.length)]);
}
// Fisher-Yates shuffle
for (let i = out.length - 1; i > 0; i--) {
const j = secureRandomInt(i + 1);
[out[i], out[j]] = [out[j], out[i]];
}
return out.join('');
}
function estimateEntropy(len, charsetSize) {
if (charsetSize <= 0) return 0;
return len * Math.log2(charsetSize);
}
function updatePreviewStats() {
const chars = buildCharset();
const charsetSize = chars.length;
const len = Number(lengthRange.value);
const entropy = estimateEntropy(len, charsetSize);
charsetSizeEl.textContent = charsetSize + ' characters';
entropyEl.textContent = entropy > 0 ? entropy.toFixed(1) + ' bits' : '— bits';
// Strength meter percentage (capped at 128 bits)
let pct = Math.min(100, Math.round((entropy / 128) * 100));
meterFill.style.width = pct + '%';
let label = '—';
if (entropy <= 28) label = 'Very weak';
else if (entropy <= 35) label = 'Weak';
else if (entropy <= 59) label = 'Moderate';
else if (entropy <= 127) label = 'Strong';
else label = 'Very strong';
strengthText.textContent = label;
// Color gradient based on strength
if (pct < 25) meterFill.style.background = 'linear-gradient(90deg, #ff6b6b, #ff9b6b)';
else if (pct < 50) meterFill.style.background = 'linear-gradient(90deg, #ffb347, #ffd56b)';
else if (pct < 75) meterFill.style.background = 'linear-gradient(90deg, #7bd389, #ffd56b)';
else meterFill.style.background = 'linear-gradient(90deg, #7bd389, #06b6d4)';
}
function showPassword(pw) {
passwordEl.textContent = pw || '—';
}
function generateAndUpdate() {
const len = Number(lengthRange.value);
const pw = generatePassword(len);
showPassword(pw);
updatePreviewStats();
}
// Event listeners
generateBtn.addEventListener('click', generateAndUpdate);
generateBtn2.addEventListener('click', generateAndUpdate);
copyBtn.addEventListener('click', async () => {
const txt = passwordEl.textContent;
if (!txt || txt === '—') return;
try {
await navigator.clipboard.writeText(txt);
copyBtn.textContent = 'Copied';
setTimeout(() => copyBtn.textContent = 'Copy', 1200);
} catch (err) {
// Fallback for older browsers
const ta = document.createElement('textarea');
ta.value = txt;
document.body.appendChild(ta);
ta.select();
try {
document.execCommand('copy');
copyBtn.textContent = 'Copied';
setTimeout(() => copyBtn.textContent = 'Copy', 1200);
} catch (e) {}
ta.remove();
}
});
copyBtn2.addEventListener('click', () => copyBtn.click());
// Update stats when toggling character type options
[upper, lower, numbers, symbols, noSimilar].forEach(el => {
el.addEventListener('change', updatePreviewStats);
});
// Initialize
updateLengthUI();
updatePreviewStats();
generateAndUpdate();