-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHash_Generator.html
More file actions
560 lines (495 loc) · 19.7 KB
/
Copy pathHash_Generator.html
File metadata and controls
560 lines (495 loc) · 19.7 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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Hash Generator</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js"></script>
<style>
/* Color scheme: dark mode */
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #121212;
color: #e0e0e0;
}
.container {
max-width: 600px;
margin: auto;
}
.file-upload {
border: 2px dashed #444;
padding: 20px;
text-align: center;
margin-bottom: 20px;
background-color: #1e1e1e;
cursor: pointer;
transition: border-color 0.3s;
}
.file-upload:hover {
border-color: #666;
}
.file-upload.dragover {
border-color: #888;
background-color: #252525;
}
.hash-result, .file-info {
margin-top: 20px;
padding: 10px;
border: 1px solid #333;
background-color: #1e1e1e;
display: none;
}
.hash-result.show, .file-info.show {
display: block;
}
button {
margin-right: 10px;
padding: 8px 16px;
background-color: #333;
color: #e0e0e0;
border: 1px solid #444;
cursor: pointer;
border-radius: 4px;
}
button:hover {
background-color: #444;
}
button:disabled {
background-color: #222;
cursor: not-allowed;
opacity: 0.6;
}
pre {
color: #e0e0e0;
word-break: break-all;
white-space: pre-wrap;
}
input[type="text"] {
background-color: #1e1e1e;
color: #e0e0e0;
border: 1px solid #444;
padding: 8px;
border-radius: 4px;
width: 70%;
}
select {
background-color: #1e1e1e;
color: #e0e0e0;
border: 1px solid #444;
padding: 4px;
border-radius: 4px;
}
.algorithm-section {
margin: 20px 0;
}
.algorithm-note {
font-size: 0.9em;
color: #aaa;
margin-top: 5px;
}
.security-warning {
color: #ff9999;
}
.compare-hash {
margin-top: 20px;
}
.status {
padding: 8px;
border-radius: 4px;
margin: 10px 0;
}
.status.success {
background-color: #1a4d1a;
border: 1px solid #2d7d2d;
}
.status.error {
background-color: #4d1a1a;
border: 1px solid #7d2d2d;
}
.status.warning {
background-color: #4d4d1a;
border: 1px solid #7d7d2d;
}
.spinner {
border: 2px solid #333;
border-top: 2px solid #666;
border-radius: 50%;
width: 16px;
height: 16px;
animation: spin 1s linear infinite;
display: inline-block;
margin-right: 8px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.progress-bar {
width: 100%;
height: 6px;
background-color: #333;
border-radius: 3px;
margin: 10px 0;
overflow: hidden;
}
.progress-fill {
height: 100%;
background-color: #666;
width: 0%;
transition: width 0.3s ease;
}
</style>
</head>
<body>
<div class="container">
<h1>File Hash Generator</h1>
<div class="file-upload" id="drop-area">
<p>Click here or drag & drop a file</p>
<input type="file" id="file-input" style="display: none;" accept="*/*">
<p>Maximum file size: 1GB</p>
</div>
<div class="algorithm-section">
<label for="hash-algorithm">Select Hash Algorithm:</label>
<select id="hash-algorithm">
<option value="MD5">MD5</option>
<option value="SHA-1">SHA-1</option>
<option value="SHA-256" selected>SHA-256</option>
<option value="SHA-384">SHA-384</option>
<option value="SHA-512">SHA-512</option>
<option value="SHA3-256">SHA3-256</option>
<option value="SHA3-384">SHA3-384</option>
<option value="SHA3-512">SHA3-512</option>
</select>
<div class="algorithm-note">
<span class="security-warning">⚠️ MD5 and SHA-1 are cryptographically broken</span> - use for file integrity only, not security
</div>
</div>
<div class="file-info" id="file-info">
<h2>File Information</h2>
<p>Name: <span id="file-name"></span></p>
<p>Size: <span id="file-size"></span></p>
<p>Type: <span id="file-type"></span></p>
<p>Modified: <span id="file-modified"></span></p>
</div>
<button id="calculate-btn" disabled>Calculate Hash</button>
<div id="progress-container" style="display: none;">
<div class="progress-bar">
<div class="progress-fill" id="progress-fill"></div>
</div>
</div>
<div class="hash-result" id="hash-result">
<h2>Hash Result</h2>
<pre id="hash-output"></pre>
<button id="copy-btn">Copy Hash</button>
</div>
<div class="compare-hash">
<h2>Compare Hash</h2>
<input type="text" id="compare-input" placeholder="Enter hash to compare">
<button id="compare-btn">Compare</button>
<div id="compare-result"></div>
</div>
</div>
<script>
// Check for required APIs and libraries
function checkBrowserSupport() {
const issues = [];
if (!window.crypto || !window.crypto.subtle) {
issues.push('Web Crypto API not supported');
}
if (!window.File || !window.FileReader || !window.ArrayBuffer) {
issues.push('File API not supported');
}
if (typeof CryptoJS === 'undefined') {
issues.push('CryptoJS library failed to load');
}
return issues;
}
// Initialize application
function initializeApp() {
const supportIssues = checkBrowserSupport();
if (supportIssues.length > 0) {
showError(`Browser compatibility issues: ${supportIssues.join(', ')}`);
return false;
}
return true;
}
// DOM element references
const dropArea = document.getElementById('drop-area');
const fileInput = document.getElementById('file-input');
const hashAlgorithm = document.getElementById('hash-algorithm');
const calculateBtn = document.getElementById('calculate-btn');
const copyBtn = document.getElementById('copy-btn');
const compareBtn = document.getElementById('compare-btn');
const fileName = document.getElementById('file-name');
const fileSize = document.getElementById('file-size');
const fileType = document.getElementById('file-type');
const fileModified = document.getElementById('file-modified');
const hashOutput = document.getElementById('hash-output');
const compareInput = document.getElementById('compare-input');
const compareResult = document.getElementById('compare-result');
const fileInfo = document.getElementById('file-info');
const hashResult = document.getElementById('hash-result');
const progressContainer = document.getElementById('progress-container');
const progressFill = document.getElementById('progress-fill');
let selectedFile = null;
let currentHash = '';
// Utility functions
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
function showError(message) {
compareResult.innerHTML = `<div class="status error">${message}</div>`;
}
function showSuccess(message) {
compareResult.innerHTML = `<div class="status success">${message}</div>`;
}
function showWarning(message) {
compareResult.innerHTML = `<div class="status warning">${message}</div>`;
}
function clearResults() {
hashResult.classList.remove('show');
hashOutput.textContent = '';
compareResult.innerHTML = '';
compareInput.value = '';
currentHash = '';
progressContainer.style.display = 'none';
progressFill.style.width = '0%';
}
function isValidHash(hash, algorithm) {
if (!hash || typeof hash !== 'string') return false;
const expectedLengths = {
'MD5': 32,
'SHA-1': 40,
'SHA-256': 64,
'SHA-384': 96,
'SHA-512': 128,
'SHA3-256': 64,
'SHA3-384': 96,
'SHA3-512': 128
};
const hexPattern = /^[a-f0-9]+$/i;
const expectedLength = expectedLengths[algorithm];
return expectedLength && hash.length === expectedLength && hexPattern.test(hash);
}
// File handling
function handleFile() {
selectedFile = fileInput.files[0];
if (!selectedFile) {
return;
}
// Check file size limit (1GB)
if (selectedFile.size > 1024 * 1024 * 1024) {
showError('File exceeds 1GB limit.');
selectedFile = null;
calculateBtn.disabled = true;
fileInfo.classList.remove('show');
return;
}
// Warn about memory usage for large files
if (selectedFile.size > 500 * 1024 * 1024) { // 500MB
showWarning('Large file detected. Processing may be slow and use significant memory.');
}
// Display file information
fileName.textContent = selectedFile.name;
fileSize.textContent = formatFileSize(selectedFile.size);
fileType.textContent = selectedFile.type || 'Unknown';
try {
fileModified.textContent = new Date(selectedFile.lastModified).toLocaleString();
} catch (e) {
fileModified.textContent = 'Unknown';
}
// Show file info and enable calculate button
fileInfo.classList.add('show');
calculateBtn.disabled = false;
// Clear previous results
clearResults();
}
// Hash calculation
async function calculateHash() {
if (!selectedFile) {
showError('Please select a file first.');
return;
}
const algo = hashAlgorithm.value;
calculateBtn.disabled = true;
calculateBtn.innerHTML = '<span class="spinner"></span>Calculating...';
progressContainer.style.display = 'block';
try {
// Simulate progress for user feedback
progressFill.style.width = '10%';
const buffer = await selectedFile.arrayBuffer();
progressFill.style.width = '50%';
let hashHex = '';
if (algo === 'MD5' || algo.startsWith('SHA3')) {
// Use crypto-js for MD5 and SHA3
if (typeof CryptoJS === 'undefined') {
throw new Error('CryptoJS library not available for ' + algo);
}
const wordArray = CryptoJS.lib.WordArray.create(buffer);
progressFill.style.width = '70%';
try {
switch (algo) {
case 'MD5':
hashHex = CryptoJS.MD5(wordArray).toString();
break;
case 'SHA3-256':
hashHex = CryptoJS.SHA3(wordArray, { outputLength: 256 }).toString();
break;
case 'SHA3-384':
hashHex = CryptoJS.SHA3(wordArray, { outputLength: 384 }).toString();
break;
case 'SHA3-512':
hashHex = CryptoJS.SHA3(wordArray, { outputLength: 512 }).toString();
break;
default:
throw new Error('Unsupported algorithm: ' + algo);
}
} catch (cryptoError) {
throw new Error(`${algo} calculation failed: ${cryptoError.message}`);
}
} else {
// Use Web Crypto API for SHA-1, SHA-256, SHA-384, SHA-512
try {
const hashBuffer = await crypto.subtle.digest(algo, buffer);
progressFill.style.width = '70%';
const hashArray = Array.from(new Uint8Array(hashBuffer));
hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
} catch (cryptoError) {
throw new Error(`${algo} calculation failed: ${cryptoError.message}`);
}
}
if (!hashHex) {
throw new Error('Hash calculation returned empty result');
}
progressFill.style.width = '100%';
hashOutput.textContent = hashHex;
currentHash = hashHex;
hashResult.classList.add('show');
setTimeout(() => {
progressContainer.style.display = 'none';
progressFill.style.width = '0%';
}, 1000);
} catch (error) {
showError(`Error calculating hash: ${error.message}`);
hashOutput.textContent = '';
currentHash = '';
progressContainer.style.display = 'none';
progressFill.style.width = '0%';
} finally {
calculateBtn.disabled = false;
calculateBtn.textContent = 'Calculate Hash';
}
}
// Copy hash to clipboard
async function copyHashToClipboard() {
const text = hashOutput.textContent.trim();
if (!text) {
showError('No hash to copy.');
return;
}
try {
await navigator.clipboard.writeText(text);
showSuccess('Hash copied to clipboard!');
} catch (error) {
// Fallback for older browsers or when clipboard API fails
try {
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.opacity = '0';
document.body.appendChild(textArea);
textArea.select();
const successful = document.execCommand('copy');
document.body.removeChild(textArea);
if (successful) {
showSuccess('Hash copied to clipboard!');
} else {
throw new Error('Copy command failed');
}
} catch (fallbackError) {
showError('Failed to copy hash. Please copy manually.');
}
}
}
// Compare hashes
function compareHashes() {
const inputHash = compareInput.value.trim();
if (!currentHash) {
showError('Please calculate a hash first.');
return;
}
if (!inputHash) {
showError('Please enter a hash to compare.');
return;
}
const algorithm = hashAlgorithm.value;
if (!isValidHash(inputHash, algorithm)) {
const expectedLengths = {
'MD5': '32',
'SHA-1': '40',
'SHA-256': '64',
'SHA-384': '96',
'SHA-512': '128',
'SHA3-256': '64',
'SHA3-384': '96',
'SHA3-512': '128'
};
const expectedLength = expectedLengths[algorithm];
showError(`Invalid ${algorithm} hash format. Expected ${expectedLength} hexadecimal characters.`);
return;
}
const inputLower = inputHash.toLowerCase();
const computedLower = currentHash.toLowerCase();
if (inputLower === computedLower) {
showSuccess('✓ Hashes match! File integrity verified.');
} else {
showError('✗ Hashes do not match. File may be corrupted or modified.');
}
}
// Event listeners setup
function setupEventListeners() {
// File selection events
dropArea.addEventListener('click', () => fileInput.click());
fileInput.addEventListener('change', handleFile);
// Drag and drop events
dropArea.addEventListener('dragover', (e) => {
e.preventDefault();
dropArea.classList.add('dragover');
});
dropArea.addEventListener('dragleave', (e) => {
e.preventDefault();
dropArea.classList.remove('dragover');
});
dropArea.addEventListener('drop', (e) => {
e.preventDefault();
dropArea.classList.remove('dragover');
if (e.dataTransfer && e.dataTransfer.files && e.dataTransfer.files.length > 0) {
fileInput.files = e.dataTransfer.files;
handleFile();
}
});
// Button events
calculateBtn.addEventListener('click', calculateHash);
copyBtn.addEventListener('click', copyHashToClipboard);
compareBtn.addEventListener('click', compareHashes);
// Keyboard events
compareInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
compareHashes();
}
});
}
// Initialize application
document.addEventListener('DOMContentLoaded', () => {
if (initializeApp()) {
setupEventListeners();
}
});
</script>
</body>
</html>