-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
507 lines (474 loc) · 17 KB
/
Copy pathindex.html
File metadata and controls
507 lines (474 loc) · 17 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>绝对安全的 2FA-TOTP 验证码生成器</title>
<link rel="icon" type="image/png" href="2FApng.png">
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
background: #f5f7fa;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 20px;
}
.container {
background: white;
border-radius: 16px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);
padding: 32px 28px;
max-width: 460px;
width: 100%;
}
h1 {
font-size: 22px;
font-weight: 600;
color: #1a1a2e;
text-align: center;
margin-bottom: 6px;
}
.subtitle {
text-align: center;
color: #6b7280;
font-size: 14px;
margin-bottom: 28px;
border-bottom: 1px solid #e5e7eb;
padding-bottom: 16px;
}
.field {
margin-bottom: 20px;
}
.field label {
display: block;
font-weight: 500;
font-size: 14px;
color: #374151;
margin-bottom: 6px;
}
.field input {
width: 100%;
padding: 10px 14px;
border: 1px solid #d1d5db;
border-radius: 8px;
font-size: 16px;
font-family: monospace;
transition: border 0.2s;
background: #f9fafb;
}
.field input:focus {
outline: none;
border-color: #6366f1;
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
background: white;
}
.hint {
font-size: 12px;
color: #9ca3af;
margin-top: 4px;
}
.token-box {
background: #f3f4f6;
border-radius: 12px;
padding: 18px 10px;
text-align: center;
margin: 10px 0 16px;
border: 1px dashed #d1d5db;
}
.token-number {
font-size: 56px;
font-weight: 700;
font-family: "SF Mono", "Menlo", monospace;
letter-spacing: 6px;
color: #1e293b;
user-select: all;
transition: color 0.2s;
}
.token-number.expiring {
color: #ef4444;
}
.progress-bar {
width: 100%;
height: 6px;
background: #e5e7eb;
border-radius: 3px;
overflow: hidden;
margin: 6px 0 12px;
}
.progress-bar .fill {
height: 100%;
background: #6366f1;
border-radius: 3px;
transition: width 0.3s linear;
}
.timer-row {
display: flex;
justify-content: space-between;
font-size: 13px;
color: #6b7280;
margin-bottom: 18px;
}
.actions {
display: flex;
gap: 10px;
justify-content: center;
flex-wrap: wrap;
}
.btn {
padding: 10px 24px;
border: none;
border-radius: 8px;
font-weight: 500;
font-size: 15px;
cursor: pointer;
background: #f3f4f6;
color: #1f2937;
transition: background 0.15s, transform 0.1s;
}
.btn-primary {
background: #6366f1;
color: white;
}
.btn-primary:hover {
background: #4f46e5;
}
.btn-primary:active {
transform: scale(0.96);
}
.btn-secondary {
background: #e5e7eb;
}
.btn-secondary:hover {
background: #d1d5db;
}
.btn-success {
background: #10b981;
color: white;
}
.btn-success:hover {
background: #059669;
}
.btn-danger {
background: #ef4444;
color: white;
}
.btn-danger:hover {
background: #dc2626;
}
.status-msg {
text-align: center;
font-size: 13px;
margin-top: 16px;
color: #6b7280;
min-height: 20px;
}
.status-msg.error {
color: #dc2626;
}
.footer {
margin-top: 24px;
text-align: center;
font-size: 12px;
color: #9ca3af;
border-top: 1px solid #e5e7eb;
padding-top: 16px;
}
.save-option {
display: flex;
align-items: center;
gap: 8px;
margin-top: 8px;
font-size: 13px;
color: #4b5563;
}
.save-option input[type="checkbox"] {
width: 16px;
height: 16px;
accent-color: #6366f1;
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<h1>🔐 2FA-TOTP 验证码生成器</h1>
<div class="subtitle">绝对安全 · 密钥永不离开</div>
<div class="field">
<label for="secretInput">你的 Base32 密钥</label>
<input type="text" id="secretInput" placeholder="例如 JBSWY3DPEHPK3PXP" spellcheck="false" autocomplete="off">
<div class="hint">支持带空格(自动去除),大小写不敏感</div>
</div>
<div class="save-option">
<input type="checkbox" id="saveKeyCheck">
<label for="saveKeyCheck">保存密钥到浏览器本地(下次自动填充),谨慎选择!</label>
</div>
<div class="token-box">
<div id="tokenDisplay" class="token-number">------</div>
<div class="progress-bar"><div id="progressFill" class="fill" style="width: 100%;"></div></div>
<div class="timer-row">
<span id="timeLeftLabel">⏳ 剩余 30 秒</span>
<span id="currentTimeLabel">🕒 本地时间</span>
</div>
</div>
<div class="actions">
<button class="btn btn-primary" id="copyBtn">📋 复制验证码</button>
<button class="btn btn-secondary" id="refreshBtn">🔄 刷新</button>
<button class="btn btn-danger" id="clearBtn">🗑️ 清除密钥</button>
</div>
<div id="statusMsg" class="status-msg">✓ 就绪</div>
<div class="footer">
基于 TOTP (RFC 6238) · 使用 Web Crypto API · 无任何网络请求 · 绝对安全
</div>
</div>
<script>
(function() {
"use strict";
// ----- DOM 引用 -----
const secretInput = document.getElementById('secretInput');
const tokenDisplay = document.getElementById('tokenDisplay');
const progressFill = document.getElementById('progressFill');
const timeLeftLabel = document.getElementById('timeLeftLabel');
const currentTimeLabel = document.getElementById('currentTimeLabel');
const copyBtn = document.getElementById('copyBtn');
const refreshBtn = document.getElementById('refreshBtn');
const clearBtn = document.getElementById('clearBtn');
const statusMsg = document.getElementById('statusMsg');
const saveKeyCheck = document.getElementById('saveKeyCheck');
// ----- 常量 -----
const PERIOD = 30;
const DIGITS = 6;
// ----- Base32 解码(RFC 4648)-----
function base32Decode(str) {
const cleaned = str.replace(/[^A-Z2-7]/gi, '').toUpperCase();
if (!cleaned) return null;
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
const length = cleaned.length;
const bytes = [];
let bits = 0;
let value = 0;
let index = 0;
for (let i = 0; i < length; i++) {
const char = cleaned[i];
const val = alphabet.indexOf(char);
if (val === -1) return null;
value = (value << 5) | val;
bits += 5;
if (bits >= 8) {
bits -= 8;
bytes.push((value >> bits) & 0xFF);
value &= (1 << bits) - 1;
}
}
return bytes;
}
// ----- HMAC-SHA1(使用 Web Crypto API)-----
async function hmacSha1(keyBytes, messageBytes) {
const cryptoKey = await window.crypto.subtle.importKey(
'raw',
new Uint8Array(keyBytes),
{ name: 'HMAC', hash: 'SHA-1' },
false,
['sign']
);
const signature = await window.crypto.subtle.sign(
'HMAC',
cryptoKey,
new Uint8Array(messageBytes)
);
return new Uint8Array(signature);
}
// ----- TOTP 核心(已修复)-----
async function generateTOTP(secretKey) {
if (!secretKey) return null;
const keyBytes = base32Decode(secretKey);
if (!keyBytes) return null;
const now = Math.floor(Date.now() / 1000);
const counter = Math.floor(now / PERIOD);
// 构造 8 字节大端整数(counter)
const msgBytes = new Uint8Array(8);
let temp = BigInt(counter);
for (let i = 7; i >= 0; i--) {
msgBytes[i] = Number(temp & BigInt(0xFF));
temp >>= BigInt(8); // ✅ 用 temp 变量,不修改 counter 本身
}
const hmac = await hmacSha1(keyBytes, msgBytes);
const offset = hmac[19] & 0x0F;
const binary = (hmac[offset] & 0x7F) << 24 |
(hmac[offset+1] & 0xFF) << 16 |
(hmac[offset+2] & 0xFF) << 8 |
(hmac[offset+3] & 0xFF);
const otp = binary % (10 ** DIGITS);
return otp.toString().padStart(DIGITS, '0');
}
// ----- 状态 -----
let currentToken = null;
let timerInterval = null;
// ----- 更新显示 -----
async function updateToken() {
const secret = secretInput.value.trim();
if (!secret) {
tokenDisplay.textContent = '------';
tokenDisplay.classList.remove('expiring');
progressFill.style.width = '100%';
timeLeftLabel.textContent = '⏳ 请输入密钥';
currentTimeLabel.textContent = '';
currentToken = null;
return;
}
try {
const token = await generateTOTP(secret);
if (token === null) {
statusMsg.textContent = '❌ 密钥格式错误(请检查是否为 Base32)';
statusMsg.className = 'status-msg error';
tokenDisplay.textContent = '❌';
currentToken = null;
return;
}
currentToken = token;
statusMsg.textContent = '✓ 验证码已生成';
statusMsg.className = 'status-msg';
tokenDisplay.textContent = token;
const now = Math.floor(Date.now() / 1000);
const elapsed = now % PERIOD;
const remaining = PERIOD - elapsed;
const pct = (remaining / PERIOD) * 100;
progressFill.style.width = pct + '%';
timeLeftLabel.textContent = `⏳ 剩余 ${remaining} 秒`;
if (remaining <= 5) {
tokenDisplay.classList.add('expiring');
} else {
tokenDisplay.classList.remove('expiring');
}
const d = new Date();
currentTimeLabel.textContent = `🕒 ${d.toLocaleTimeString()}`;
} catch (e) {
console.error(e);
statusMsg.textContent = '⚠️ 生成失败:' + e.message;
statusMsg.className = 'status-msg error';
tokenDisplay.textContent = '⚠️';
currentToken = null;
}
}
// ----- 定时器 -----
function startTimer() {
if (timerInterval) clearInterval(timerInterval);
timerInterval = setInterval(() => {
const secret = secretInput.value.trim();
if (!secret) {
progressFill.style.width = '100%';
timeLeftLabel.textContent = '⏳ 请输入密钥';
return;
}
const now = Math.floor(Date.now() / 1000);
const elapsed = now % PERIOD;
const remaining = PERIOD - elapsed;
const pct = (remaining / PERIOD) * 100;
progressFill.style.width = pct + '%';
timeLeftLabel.textContent = `⏳ 剩余 ${remaining} 秒`;
if (remaining <= 5) {
tokenDisplay.classList.add('expiring');
} else {
tokenDisplay.classList.remove('expiring');
}
const d = new Date();
currentTimeLabel.textContent = `🕒 ${d.toLocaleTimeString()}`;
if (remaining === PERIOD || remaining === 0) {
updateToken();
}
}, 1000);
}
// ----- 手动刷新 -----
async function refresh() {
await updateToken();
if (!timerInterval) startTimer();
}
// ----- 复制 -----
async function copyToken() {
const text = tokenDisplay.textContent;
if (!text || text === '------' || text === '❌' || text === '⚠️') {
statusMsg.textContent = '⚠️ 没有有效的验证码可复制';
statusMsg.className = 'status-msg error';
return;
}
try {
await navigator.clipboard.writeText(text);
statusMsg.textContent = '✅ 已复制到剪贴板!';
statusMsg.className = 'status-msg';
} catch (e) {
const range = document.createRange();
range.selectNode(tokenDisplay);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
statusMsg.textContent = '✅ 已复制(降级方式)';
statusMsg.className = 'status-msg';
}
}
// ----- 清除密钥 -----
function clearSecret() {
secretInput.value = '';
if (saveKeyCheck.checked) {
localStorage.removeItem('totp_secret');
}
updateToken();
statusMsg.textContent = '🗑️ 密钥已清除';
statusMsg.className = 'status-msg';
tokenDisplay.textContent = '------';
progressFill.style.width = '100%';
timeLeftLabel.textContent = '⏳ 请输入密钥';
currentTimeLabel.textContent = '';
}
// ----- 保存/加载 -----
function loadSavedSecret() {
try {
const saved = localStorage.getItem('totp_secret');
if (saved) {
secretInput.value = saved;
saveKeyCheck.checked = true;
return true;
}
} catch (e) {}
return false;
}
secretInput.addEventListener('input', function() {
if (saveKeyCheck.checked) {
localStorage.setItem('totp_secret', this.value.trim());
}
updateToken();
});
saveKeyCheck.addEventListener('change', function() {
if (this.checked) {
const val = secretInput.value.trim();
if (val) localStorage.setItem('totp_secret', val);
} else {
localStorage.removeItem('totp_secret');
}
});
copyBtn.addEventListener('click', copyToken);
refreshBtn.addEventListener('click', refresh);
clearBtn.addEventListener('click', clearSecret);
(async function init() {
const hasSaved = loadSavedSecret();
statusMsg.textContent = hasSaved ? '🔓 已加载保存的密钥' : '💡 输入密钥后自动生成';
await updateToken();
startTimer();
setInterval(() => { updateToken(); }, PERIOD * 1000);
})();
window.addEventListener('beforeunload', function() {
if (saveKeyCheck.checked) {
localStorage.setItem('totp_secret', secretInput.value.trim());
}
});
})();
</script>
</body>
</html>