-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
134 lines (110 loc) · 4.24 KB
/
script.js
File metadata and controls
134 lines (110 loc) · 4.24 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
let currentHash = '';
let currentFilename = '';
async function generateHash() {
const fileInput = document.getElementById('fileInput');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.textContent = "Please select a file.";
resultDiv.classList.remove('hidden');
return;
}
const file = fileInput.files[0];
currentFilename = file.name;
const arrayBuffer = await file.arrayBuffer();
const hashBuffer = await crypto.subtle.digest('SHA-256', arrayBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
currentHash = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
resultDiv.innerHTML = `<strong>SHA-256 Hash:</strong><br>${currentHash}`;
resultDiv.classList.remove('hidden');
saveToHistory(currentFilename, currentHash);
updateHistoryMenu();
}
function compareHash() {
const knownHash = document.getElementById('knownHash').value.trim();
const compareSource = document.getElementById('compareSource').value;
const comparisonDiv = document.getElementById('comparisonResult');
if (!knownHash) {
comparisonDiv.textContent = "Please enter a hash to compare.";
comparisonDiv.classList.remove('hidden');
return;
}
let targetHash = '';
let sourceLabel = '';
if (compareSource === 'current') {
if (!currentHash) {
comparisonDiv.textContent = "No current file hash available. Please generate one first.";
comparisonDiv.classList.remove('hidden');
return;
}
targetHash = currentHash;
sourceLabel = `current file: ${currentFilename}`;
} else if (compareSource === 'history') {
const history = JSON.parse(localStorage.getItem('hashHistory') || '[]');
if (history.length === 0) {
comparisonDiv.textContent = "No history available to compare.";
comparisonDiv.classList.remove('hidden');
return;
}
targetHash = history[0].hash; // Compare with most recent history entry
sourceLabel = `history file: ${history[0].filename}`;
}
if (knownHash === targetHash) {
comparisonDiv.innerHTML = `<span style="color:green;">✅ Match with ${sourceLabel}</span>`;
} else {
comparisonDiv.innerHTML = `<span style="color:red;">❌ No match with ${sourceLabel}</span>`;
}
comparisonDiv.classList.remove('hidden');
}
function checkHashInHistory(hashToFind) {
const history = JSON.parse(localStorage.getItem('hashHistory') || '[]');
return history.find(item => item.hash === hashToFind);
}
function saveToHistory(filename, hash) {
const history = JSON.parse(localStorage.getItem('hashHistory') || '[]');
history.unshift({ filename, hash });
localStorage.setItem('hashHistory', JSON.stringify(history.slice(0, 10))); // keep last 10
}
function updateHistoryMenu() {
const historyList = document.getElementById('historyList');
const history = JSON.parse(localStorage.getItem('hashHistory') || '[]');
historyList.innerHTML = '';
if (history.length === 0) {
historyList.classList.add('hidden');
return;
}
historyList.classList.remove('hidden');
history.forEach((item, index) => {
const li = document.createElement('li');
li.classList.add('history-item');
li.innerHTML = `
<div class="history-content">
<div class="hash-info">
<strong>${item.filename}</strong><br>
<code>${item.hash}</code>
</div>
<button class="compare-btn" onclick="compareToCurrent('${item.hash}', '${item.filename}')">Compare</button>
</div>
`;
historyList.appendChild(li);
});
}
// Load history on page load
updateHistoryMenu();
function clearHistory() {
localStorage.removeItem('hashHistory');
updateHistoryMenu();
}
function compareToCurrent(historyHash, historyFilename) {
const comparisonDiv = document.getElementById('comparisonResult');
if (!currentHash) {
comparisonDiv.textContent = "No current file hash available. Please generate one first.";
comparisonDiv.classList.remove('hidden');
return;
}
if (currentHash === historyHash) {
comparisonDiv.innerHTML = `<span style="color:green;">Match with history file: ${historyFilename}</span>`;
} else {
comparisonDiv.innerHTML = `<span style="color:red;">No match with history file: ${historyFilename}</span>`;
}
comparisonDiv.classList.remove('hidden');
}