blob: 5207ac64497c11cc503f241f4dafb237470cb946 (
plain)
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
|
const text = 'An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.';
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
async function digestMessage(message) {
const msgUint8 = new TextEncoder().encode(message); // encode as (utf-8) Uint8Array
const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8); // hash the message
const hashArray = Array.from(new Uint8Array(hashBuffer)); // convert buffer to byte array
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, '0')).join(''); // convert bytes to hex string
return hashHex;
}
const HARDNESS = 4;
async function findPrefix(challenge) {
while (true) {
let prefix = makeid(32);
let digestHex = await digestMessage(prefix + challenge);
if (digestHex.startsWith("0".repeat(HARDNESS))) {
return [prefix + challenge, digestHex];
}
}
}
const suffix = "p12IyPB2dZsmBHYZyaEMrR5OUxqNc5Z9Cijal+9/iuQ=";
findPrefix(suffix).then(console.log);
|