-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpow.html
More file actions
44 lines (38 loc) · 1.22 KB
/
Copy pathpow.html
File metadata and controls
44 lines (38 loc) · 1.22 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Proof of Work</title>
</head>
<body>
<h1>Solving Proof of Work...</h1>
<script>
async function sha256(message) {
const encoder = new TextEncoder();
const data = encoder.encode(message);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
async function solvePoW(difficulty = 1) {
const targetPrefix = '0'.repeat(difficulty);
const baseData = 'PoW-' + Math.random().toString(36).slice(2);
let nonce = 0;
while (true) {
const data = baseData + nonce;
const hash = await sha256(data);
if (hash.startsWith(targetPrefix)) {
const message = `✅ Proof of Work Solved!\n\nNonce: ${nonce}\nHash: ${hash}`;
alert(message);
console.log(message);
break;
}
nonce++;
// Optional: prevent UI freeze every 10k tries
if (nonce % 10000 === 0) await new Promise(r => setTimeout(r, 0));
}
}
solvePoW(4); // Difficulty = 1 (very quick)
</script>
</body>
</html>