@trustcomponent/trustcaptcha-frontend
Version:
TrustCaptcha – Privacy-first CAPTCHA solution. GDPR-compliant, bot protection made in Europe.
30 lines (29 loc) • 1.18 kB
JavaScript
self.addEventListener('message', async (event) => {
const { task, difficulty } = event.data;
postMessage(await (async function (task, difficulty) {
const inputBytes = Uint8Array.from(atob(task.input), c => c.charCodeAt(0));
let nonce = 0;
while (true) {
const nonceStr = `tcn${nonce}`;
const nonceBytes = new TextEncoder().encode(nonceStr);
const data = new Uint8Array(inputBytes.length + nonceBytes.length);
data.set(inputBytes);
data.set(nonceBytes, inputBytes.length);
const hashBytes = new Uint8Array(await crypto.subtle.digest('SHA-256', data));
let bits = 0;
for (let i = 0; i < hashBytes.length; i++) {
const byte = hashBytes[i];
if (byte === 0)
bits += 8;
else {
bits += Math.clz32(byte) - 24;
break;
}
}
if (bits === difficulty)
return { number: task.number, nonce: nonceStr };
nonce++;
}
})(task, difficulty));
});
//# sourceMappingURL=pow-worker.js.map