-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.html
More file actions
309 lines (281 loc) · 13.1 KB
/
verify.html
File metadata and controls
309 lines (281 loc) · 13.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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="本地文件SHA-256校验工具,完全在浏览器中运行,不上传任何数据">
<title>文件校验(SHA‑256) — 本地校验器</title>
<style>
:root {
--bg: #f6f8fb; --card: #fff; --accent: #0ea5e9; --accent-hover: #0284c7;
--ok: #16a34a; --bad: #dc2626; --warning: #f59e0b; --muted: #6b7280;
--border: #e6eef8; --text: #0f172a; --radius: 10px; --shadow: 0 8px 30px rgba(2, 6, 23, 0.06);
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0f172a; --card: #1e293b; --accent: #38bdf8; --accent-hover: #0ea5e9;
--ok: #22c55e; --bad: #ef4444; --warning: #f59e0b; --muted: #94a3b8;
--border: #334155; --text: #f1f5f9;
}
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Inter', system-ui, -apple-system, sans-serif;
background: var(--bg); color: var(--text); line-height: 1.6;
min-height: 100vh; padding: 18px;
}
.wrap {
max-width: 900px; margin: 0 auto; background: var(--card);
padding: 24px; border-radius: var(--radius); box-shadow: var(--shadow);
}
h1 { font-size: 1.5rem; margin-bottom: 8px; }
h2 { font-size: 1.25rem; margin: 20px 0 12px; }
.subtitle { font-size: 0.95rem; color: var(--muted); margin-bottom: 24px; }
.panel {
background: var(--bg); padding: 20px; border-radius: 8px;
margin-top: 20px; border: 1px solid var(--border);
}
label {
display: block; margin: 16px 0 8px; font-weight: 600;
font-size: 0.95rem; color: var(--text);
}
input[type="file"], input[type="text"] {
width: 100%; padding: 12px; border-radius: 6px; border: 2px solid var(--border);
background: var(--card); color: var(--text); font-family: inherit; font-size: 0.95rem;
}
input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(14, 165, 233, 0.1); }
.row { display: flex; gap: 12px; align-items: center; margin-top: 16px; flex-wrap: wrap; }
button {
background: var(--accent); color: white; border: none; padding: 12px 20px;
border-radius: 6px; cursor: pointer; font-weight: 600; font-size: 0.95rem;
display: inline-flex; align-items: center; gap: 6px; min-height: 44px;
}
button:hover { background: var(--accent-hover); }
button:disabled { opacity: 0.6; cursor: not-allowed; }
button.secondary { background: transparent; color: var(--accent); border: 2px solid var(--border); }
button.secondary:hover { background: rgba(14, 165, 233, 0.1); border-color: var(--accent); }
.result {
margin-top: 16px; padding: 16px; border-radius: 6px; font-weight: 600;
font-size: 0.95rem; display: none; border-left: 4px solid transparent;
}
.result.success { background: rgba(22, 163, 74, 0.1); border-left-color: var(--ok); color: var(--ok); }
.result.warning { background: rgba(245, 158, 11, 0.1); border-left-color: var(--warning); color: var(--warning); }
.result.error { background: rgba(220, 38, 38, 0.1); border-left-color: var(--bad); color: var(--bad); }
.result.info { background: rgba(14, 165, 233, 0.1); border-left-color: var(--accent); color: var(--accent); }
.hash-display { margin-top: 16px; }
.hash-label { font-size: 0.9rem; color: var(--muted); margin-bottom: 6px; }
.hash-box {
word-break: break-all; background: var(--card); padding: 12px; border-radius: 6px;
border: 2px solid var(--border); font-family: monospace; font-size: 0.9rem; color: var(--text);
}
.hash-box.calculated { border-color: var(--accent); background: rgba(14, 165, 233, 0.05); }
.stats { display: flex; gap: 20px; margin-top: 8px; font-size: 0.85rem; color: var(--muted); }
ol, ul { padding-left: 20px; margin: 12px 0; }
li { margin-bottom: 8px; }
code {
background: var(--bg); padding: 2px 6px; border-radius: 4px;
font-family: monospace; font-size: 0.9rem; border: 1px solid var(--border);
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.file-info { display: flex; align-items: center; gap: 12px; margin-top: 8px; font-size: 0.9rem; color: var(--muted); }
@media (max-width: 768px) {
.wrap { padding: 16px; }
.row { flex-direction: column; align-items: stretch; }
button { width: 100%; justify-content: center; }
}
</style>
</head>
<body>
<main class="wrap">
<header>
<h1>📁 文件校验(SHA‑256)</h1>
<p class="subtitle">在本地计算文件哈希并与发送方提供的哈希比对。全部在浏览器本地执行,不会上传任何文件或数据。</p>
</header>
<section class="panel">
<h2>发送方(简要步骤)</h2>
<ol>
<li>把文件保存到本地(例如 index.html)。</li>
<li>在本机计算 SHA‑256:
<ul>
<li><strong>macOS/Linux:</strong> <code>shasum -a 256 文件名</code></li>
<li><strong>Windows PowerShell:</strong> <code>Get-FileHash 文件名 -Algorithm SHA256</code></li>
<li><strong>Windows CMD:</strong> <code>certutil -hashfile 文件名 SHA256</code></li>
</ul>
</li>
<li>通过另一条渠道(电话/短信/另一款聊天)把哈希字符串发给接收方。</li>
</ol>
</section>
<section class="panel">
<h2>接收方操作 — 本地校验</h2>
<div class="file-selection">
<label for="fileInput">1) 选择要校验的本地文件</label>
<input id="fileInput" type="file" accept="*/*">
<div id="fileInfo" class="file-info" style="display: none;">
📄 <span id="fileName"></span> • <span id="fileSize"></span> • <span id="fileType"></span>
</div>
</div>
<label for="expectedHash">2) 粘贴发送方给你的 SHA‑256 字符串</label>
<input id="expectedHash" type="text"
placeholder="例如:8d1c7d86c72b6cd4fc0e55c66cda1e9d6e40f319981d3cf9f96c23436e307c9f"
autocomplete="off" spellcheck="false">
<div class="row">
<button id="calcBtn">✅ 计算并比对 SHA‑256</button>
<button id="pasteExpected" class="secondary">📋 粘贴哈希值</button>
<button id="clearBtn" class="secondary">🗑️ 清空</button>
<a href="index.html" class="back-link">↩️ 返回加密页面</a>
</div>
<div id="result" class="result" style="display: none;"></div>
<div class="hash-display">
<div class="hash-label">计算出的哈希(HEX,大写):</div>
<div id="computedHash" class="hash-box">尚未计算</div>
<div class="stats">
<span id="hashLength">长度: 0</span>
<span id="calcTime">耗时: 0ms</span>
<span id="fileSizeDisplay">文件: 0 B</span>
</div>
</div>
</section>
<section class="panel">
<h2>若校验失败怎么办</h2>
<ol>
<li><strong>不要打开该文件或输入口令。</strong> 文件可能已被篡改。</li>
<li>通过电话确认发送方的哈希是否正确;必要时重传文件并使用不同渠道发送校验值。</li>
<li>若文件来自网络且校验失败,请勿使用,联系发布者核实并检查发布渠道安全(2FA、提交记录)。</li>
<li>确保比较的哈希值格式一致(去除空格、统一大小写)。</li>
</ol>
</section>
<section class="panel">
<h2>安全提醒</h2>
<ul>
<li>🔒 所有计算都在本地浏览器中进行,数据不会上传到任何服务器</li>
<li>📱 建议在可信设备上使用此工具</li>
<li>🔑 始终通过不同渠道传输哈希值和文件本身</li>
<li>🔄 定期更新您使用的加密工具和库</li>
<li>💾 对于重要文件,建议同时计算多种哈希算法(SHA-256、SHA-512、BLAKE2)</li>
</ul>
</section>
</main>
<script>
const fileInput = document.getElementById('fileInput');
const expectedInput = document.getElementById('expectedHash');
const calcBtn = document.getElementById('calcBtn');
const pasteExpectedBtn = document.getElementById('pasteExpected');
const clearBtn = document.getElementById('clearBtn');
const resultEl = document.getElementById('result');
const computedHashEl = document.getElementById('computedHash');
const hashLengthEl = document.getElementById('hashLength');
const calcTimeEl = document.getElementById('calcTime');
const fileSizeDisplayEl = document.getElementById('fileSizeDisplay');
const fileInfoEl = document.getElementById('fileInfo');
const fileNameEl = document.getElementById('fileName');
const fileSizeEl = document.getElementById('fileSize');
const fileTypeEl = document.getElementById('fileType');
function formatBytes(bytes) {
if (bytes === 0) return '0 B';
const k = 1024, sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
function formatTime(ms) {
return ms < 1000 ? `${ms}ms` : `${(ms / 1000).toFixed(2)}s`;
}
function normalizeHash(hash) {
return hash.replace(/\s+/g, '').toUpperCase().trim();
}
function bufToHex(buffer) {
return Array.from(new Uint8Array(buffer))
.map(b => b.toString(16).padStart(2, '0'))
.join('').toUpperCase();
}
async function computeFileSHA256(file) {
const startTime = performance.now();
const arrayBuffer = await file.arrayBuffer();
const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
const endTime = performance.now();
return {
hash: bufToHex(hashBuffer),
time: endTime - startTime
};
}
function showResult(type, message) {
resultEl.className = `result ${type}`;
resultEl.textContent = message;
resultEl.style.display = 'block';
resultEl.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
}
fileInput.addEventListener('change', function() {
if (this.files.length > 0) {
const file = this.files[0];
fileNameEl.textContent = file.name;
fileSizeEl.textContent = formatBytes(file.size);
fileTypeEl.textContent = file.type || '未知类型';
fileSizeDisplayEl.textContent = `文件: ${formatBytes(file.size)}`;
fileInfoEl.style.display = 'flex';
computedHashEl.textContent = '请重新计算';
computedHashEl.className = 'hash-box';
showResult('info', '已选择新文件,请点击计算按钮');
} else {
fileInfoEl.style.display = 'none';
}
});
calcBtn.addEventListener('click', async () => {
const file = fileInput.files[0];
if (!file) {
showResult('error', '请先选择要校验的文件');
return;
}
try {
calcBtn.disabled = true;
calcBtn.textContent = '计算中...';
const { hash, time } = await computeFileSHA256(file);
computedHashEl.textContent = hash;
computedHashEl.className = 'hash-box calculated';
hashLengthEl.textContent = `长度: ${hash.length}`;
calcTimeEl.textContent = `耗时: ${formatTime(time)}`;
const expected = normalizeHash(expectedInput.value);
if (!expected) {
showResult('warning', '已计算出文件哈希,请粘贴发送方提供的哈希值进行比对');
return;
}
showResult(hash === expected ? 'success' : 'error',
hash === expected ? '✅ 校验通过:哈希一致,文件完整可信'
: '❌ 校验失败:哈希不一致!文件可能已被篡改');
} catch (error) {
showResult('error', `计算失败:${error.message || '未知错误'}`);
computedHashEl.textContent = '计算错误';
computedHashEl.className = 'hash-box';
} finally {
calcBtn.disabled = false;
calcBtn.textContent = '✅ 计算并比对 SHA‑256';
}
});
pasteExpectedBtn.addEventListener('click', async () => {
try {
const text = await navigator.clipboard.readText();
if (text) {
expectedInput.value = text.trim();
showResult('info', '已从剪贴板粘贴哈希值');
}
} catch {
showResult('error', '无法读取剪贴板');
}
});
clearBtn.addEventListener('click', () => {
fileInput.value = '';
expectedInput.value = '';
computedHashEl.textContent = '尚未计算';
computedHashEl.className = 'hash-box';
resultEl.style.display = 'none';
fileInfoEl.style.display = 'none';
hashLengthEl.textContent = '长度: 0';
calcTimeEl.textContent = '耗时: 0ms';
fileSizeDisplayEl.textContent = '文件: 0 B';
});
expectedInput.addEventListener('input', function() {
this.value = this.value.toUpperCase().replace(/[^A-F0-9]/gi, '');
});
console.log('文件校验工具已加载');
</script>
</body>
</html>