forked from jordansamuel/PASTE
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgeneratepasswd.php
More file actions
344 lines (308 loc) · 12.1 KB
/
generatepasswd.php
File metadata and controls
344 lines (308 loc) · 12.1 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/*
* File: generatepasswd.php for Paste
* Just a little Bootstrap 5 tool for the registration page.
* Fully client side generation.
* License: GPLv3
*/
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Password Generator</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">
<style>
/* Keep styles minimal*/
:root {
--brand-blue: #399BFF;
--card-bg: rgba(255,255,255,0.04);
--text-muted: #9aa4ad;
}
@media (prefers-color-scheme: light) {
:root {
--card-bg: #ffffff;
--text-muted: #6c757d;
}
}
body {
font-family: "Fira Sans", system-ui, -apple-system, Segoe UI, Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
min-height: 100vh;
display: flex;
flex-direction: column;
background:
radial-gradient(1200px 600px at 10% -10%, rgba(57,155,255,.20), transparent 60%),
radial-gradient(1200px 600px at 110% 10%, rgba(57,155,255,.15), transparent 60%);
}
.page-wrap { flex: 1 0 auto; }
.hero {
text-align: center;
margin-top: 2rem;
margin-bottom: 1.25rem;
}
.hero h1 {
font-weight: 700;
letter-spacing: .2px;
}
.hero p {
color: var(--text-muted);
margin: .5rem 0 0;
}
.generator-card {
background: var(--card-bg);
border: 1px solid rgba(255,255,255,0.08);
border-radius: 1rem;
backdrop-filter: saturate(120%) blur(6px);
}
.password-box {
font-family: "Fira Code", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
font-size: 1.125rem;
user-select: all;
letter-spacing: .2px;
}
.btn-perky {
--bs-btn-bg: var(--brand-blue);
--bs-btn-border-color: var(--brand-blue);
--bs-btn-hover-bg: #2f8ae5;
--bs-btn-hover-border-color: #2f8ae5;
--bs-btn-color: #fff;
}
.entropy-badge {
font-variant-numeric: tabular-nums;
}
.footer-note {
color: var(--text-muted);
font-size: .9rem;
}
.range-output {
min-width: 2.5ch;
text-align: right;
display: inline-block;
}
.form-check .form-text {
margin-left: 1.65rem;
margin-top: .25rem;
}
</style>
</head>
<body>
<main class="page-wrap">
<div class="container">
<div class="hero">
<h1>Generate a Secure Password</h1>
<p>Client-side generator — No data leaves your browser</p>
</div>
<div class="row justify-content-center">
<div class="col-12 col-lg-8">
<div class="generator-card shadow-sm p-3 p-md-4">
<!-- Output -->
<div class="mb-3">
<label class="form-label fw-semibold">Password</label>
<div class="input-group">
<input id="out" type="text" class="form-control password-box" readonly aria-describedby="copyBtn">
<button id="regenBtn" class="btn btn-outline-secondary" type="button" title="Regenerate">
<i class="bi bi-arrow-repeat"></i>
</button>
<button id="copyBtn" class="btn btn-perky fw-semibold" type="button" title="Copy to clipboard">
<i class="bi bi-clipboard-check"></i> Copy
</button>
</div>
<div class="d-flex align-items-center gap-3 mt-2">
<span class="badge text-bg-secondary entropy-badge" id="entropyBadge" title="Estimated entropy in bits">— bits</span>
<span id="strengthLabel" class="small"></span>
</div>
</div>
<hr class="my-4">
<!-- Controls -->
<form class="row gy-3">
<div class="col-12 col-md-6">
<label for="len" class="form-label fw-semibold">Length:
<span class="range-output" id="lenOut">16</span>
</label>
<input id="len" type="range" class="form-range" min="8" max="64" step="1" value="16" aria-describedby="lenHelp">
<div id="lenHelp" class="form-text">Longer is stronger; 16–24 is a great default.</div>
</div>
<div class="col-12 col-md-6">
<div class="row">
<div class="col-6">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="lower" checked>
<label class="form-check-label" for="lower">Lowercase (a–z)</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="upper" checked>
<label class="form-check-label" for="upper">Uppercase (A–Z)</label>
</div>
</div>
<div class="col-6">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="digits" checked>
<label class="form-check-label" for="digits">Digits (0–9)</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="symbols" checked>
<label class="form-check-label" for="symbols">Symbols (!@#$…)</label>
</div>
</div>
</div>
<div class="form-check mt-2">
<input class="form-check-input" type="checkbox" id="noAmbig" checked>
<label class="form-check-label" for="noAmbig">Avoid ambiguous characters</label>
<div class="form-text">Skips look-alikes like <code>O</code>/<code>0</code>, <code>l</code>/<code>1</code>, <code>{}</code>/<code>[]</code>.</div>
</div>
</div>
<div class="col-12">
<div class="d-flex gap-2">
<button type="button" id="generateBtn" class="btn btn-perky fw-semibold">
<i class="bi bi-magic"></i> Generate
</button>
<button type="button" id="copyBtn2" class="btn btn-outline-secondary">
<i class="bi bi-clipboard"></i> Copy
</button>
</div>
</div>
</form>
<div class="mt-4 footer-note">
Tip: Use a unique password for every site. Consider a password manager.
</div>
</div>
<div class="text-center mt-3">
<a class="small text-decoration-underline" href="/" aria-label="Back to Paste">← Back to Paste</a>
</div>
</div>
</div>
</div>
</main>
<script>
(function () {
"use strict";
const $ = (id) => document.getElementById(id);
const lowerChars = "abcdefghijklmnopqrstuvwxyz";
const upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const digitChars = "0123456789";
const symbolChars = "!@#$%^&*()-_=+[]{};:,.<>/?~";
const ambiguous = "O0oIl1|[]{}()<>`'\".,;:~";
const len = $("len");
const lenOut = $("lenOut");
const lower = $("lower");
const upper = $("upper");
const digits = $("digits");
const symbols = $("symbols");
const noAmbig = $("noAmbig");
const out = $("out");
const generateBtn = $("generateBtn");
const regenBtn = $("regenBtn");
const copyBtn = $("copyBtn");
const copyBtn2 = $("copyBtn2");
const entropyBadge = $("entropyBadge");
const strengthLabel= $("strengthLabel");
function updateLenOutput() {
lenOut.textContent = String(len.value);
}
function buildCharset() {
let set = "";
if (lower.checked) set += lowerChars;
if (upper.checked) set += upperChars;
if (digits.checked) set += digitChars;
if (symbols.checked) set += symbolChars;
if (noAmbig.checked) {
set = [...set].filter(ch => !ambiguous.includes(ch)).join("");
}
return Array.from(new Set(set)).join(""); // dedupe
}
function getRandomValues(length) {
const arr = new Uint32Array(length);
if (window.crypto && window.crypto.getRandomValues) {
window.crypto.getRandomValues(arr);
} else {
// Fallback (should almost never happen): use Math.random
for (let i = 0; i < length; i++) arr[i] = Math.floor(Math.random() * 0xFFFFFFFF);
}
return arr;
}
function generatePassword() {
const L = parseInt(len.value, 10);
let charset = buildCharset();
if (!charset) {
alert("Please select at least one character set.");
return;
}
// Ensure we sample uniformly from charset using rejection sampling
const chars = Array.from(charset);
const n = chars.length;
const outChars = [];
const rand = getRandomValues(L * 2); // buffer
let i = 0;
const max = Math.floor(0x100000000 / n) * n; // highest multiple of n below 2^32
for (let produced = 0; produced < L; ) {
if (i >= rand.length) {
// refill
const refill = getRandomValues(L);
for (let k = 0; k < refill.length; k++) rand[k] = refill[k];
i = 0;
}
const r = rand[i++];
if (r < max) {
outChars.push(chars[r % n]);
produced++;
}
}
const pwd = outChars.join("");
out.value = pwd;
updateStrength(pwd, n);
}
function log2(x) { return Math.log(x) / Math.log(2); }
function updateStrength(pwd, charsetSize) {
const L = pwd.length || 0;
// Shannon-style estimate (assuming uniform random from charset)
const bits = Math.round(L * log2(Math.max(2, charsetSize)));
entropyBadge.textContent = bits + " bits";
let label = "";
let cls = "text-secondary";
if (bits < 45) { label = "Weak"; cls = "text-danger"; }
else if (bits < 64) { label = "Fair"; cls = "text-warning"; }
else if (bits < 90) { label = "Strong"; cls = "text-success"; }
else { label = "Excellent"; cls = "text-success"; }
strengthLabel.className = "small " + cls;
strengthLabel.textContent = label + " (charset " + charsetSize + ", length " + L + ")";
}
function copyToClipboard() {
out.select();
out.setSelectionRange(0, 99999);
navigator.clipboard?.writeText(out.value).then(() => {
flashCopied(copyBtn);
flashCopied(copyBtn2);
}).catch(() => {
// Fallback
document.execCommand("copy");
flashCopied(copyBtn);
flashCopied(copyBtn2);
});
}
function flashCopied(btn) {
if (!btn) return;
const original = btn.innerHTML;
btn.innerHTML = '<i class="bi bi-check2-circle"></i> Copied';
btn.disabled = true;
setTimeout(() => {
btn.innerHTML = original;
btn.disabled = false;
}, 1200);
}
// Events
len.addEventListener("input", updateLenOutput);
generateBtn.addEventListener("click", generatePassword);
regenBtn.addEventListener("click", generatePassword);
copyBtn.addEventListener("click", copyToClipboard);
copyBtn2.addEventListener("click", copyToClipboard);
[lower, upper, digits, symbols, noAmbig].forEach(el => {
el.addEventListener("change", generatePassword);
});
// Init
updateLenOutput();
generatePassword();
})();
</script>
</body>
</html>