-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash-generator.js
65 lines (57 loc) · 2.09 KB
/
hash-generator.js
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
// This function remains unchanged, used for both live hashing and the worker
async function sha256(message) {
const msgBuffer = new TextEncoder('utf-8').encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
return hashHex;
}
// For live hashing
async function liveHash() {
const sentence = document.getElementById("baseSentence").value;
const hash = await sha256(sentence);
document.getElementById("liveHashResult").innerText = hash;
}
function findMatchingHash() {
const baseSentence = document.getElementById("baseSentence").value;
const targetPrefix = document.getElementById("targetPrefix").value;
const worker = new Worker('hashWorker.js');
worker.postMessage({
baseSentence: baseSentence,
targetPrefix: targetPrefix,
start: 0,
end: 500000
});
worker.onmessage = function(e) {
const { type, count, hash } = e.data;
if (type === 'progress') {
document.getElementById("result").innerText = `Count: ${count}, Hash: ${hash}`;
}
}
}
// For color representation
function displayHashColor(hash) {
const color = `#${hash.substring(0, 6)}`; // Taking the first 6 characters as HEX color.
document.body.style.backgroundColor = color;
}
function findMatchingHash() {
const baseSentence = document.getElementById("baseSentence").value;
const targetPrefix = document.getElementById("targetPrefix").value;
// Create 4 workers
for (let i = 0; i < 4; i++) {
const worker = new Worker('hashWorker.js');
worker.postMessage({
baseSentence: baseSentence,
targetPrefix: targetPrefix,
start: i * 250000,
end: (i+1) * 250000
});
worker.onmessage = function(e) {
const { type, count, hash } = e.data;
if (type === 'progress') {
document.getElementById("result").innerText = `Count: ${count}, Hash: ${hash}`;
displayHashColor(hash);
}
}
}
}