(function() { "use strict"; function assertAlgorithm(algorithm, allowed) { if (!allowed.includes(algorithm)) { throw new Error( `Unsupported algorithm: ${String(algorithm)}. Expected one of: ${allowed.join(", ")}.` ); } } function bufferStartsWith(buffer, prefix) { if (prefix.length > buffer.length) { return false; } for (let i = 0; i < prefix.length; i++) { if (buffer[i] !== prefix[i]) { return false; } } return true; } function bufferToHex(buffer) { return Array.from(new Uint8Array(buffer)).map((b) => b.toString(16).padStart(2, "0")).join(""); } function concatBuffers(a, b) { const out = new Uint8Array(a.length + b.length); out.set(a, 0); out.set(b, a.length); return out; } function hexToBuffer(hex) { if (hex.length % 2 !== 0) { throw new Error(`Hex string must have an even length. Got: ${hex}`); } if (!/^[0-9a-fA-F]*$/.test(hex)) { throw new Error("Hex string contains non-hex characters."); } const buffer = new Uint8Array(hex.length / 2); for (let i = 0; i < buffer.length; i++) { buffer[i] = parseInt(hex.substring(i * 2, i * 2 + 2), 16); } return buffer; } async function delay(ms) { await new Promise((resolve) => setTimeout(resolve, ms)); } function timeDuration(start) { return Math.floor((performance.now() - start) * 10) / 10; } var HmacAlgorithm = /* @__PURE__ */ ((HmacAlgorithm2) => { HmacAlgorithm2["SHA_256"] = "SHA-256"; HmacAlgorithm2["SHA_384"] = "SHA-384"; HmacAlgorithm2["SHA_512"] = "SHA-512"; return HmacAlgorithm2; })(HmacAlgorithm || {}); Object.values(HmacAlgorithm); const MAX_COUNTER = { string: Number.MAX_SAFE_INTEGER, uint32: 4294967295 }; function isValidCounter(n, mode) { return Number.isInteger(n) && n >= 0 && n <= MAX_COUNTER[mode]; } class PasswordBuffer { constructor(nonce, mode = "uint32") { this.nonce = nonce; this.mode = mode; this.buffer = new Uint8Array(this.nonce.length + this.COUNTER_BYTES); this.buffer.set(this.nonce, 0); this.dataView = new DataView(this.buffer.buffer); } nonce; mode; COUNTER_BYTES = 4; buffer; dataView; encoder = new TextEncoder(); /** * Appends the counter to the nonce buffer. * In 'string' mode, encodes the counter as a UTF-8 string. * In 'uint32' mode, writes the counter as a big-endian 32-bit integer. * Throws a RangeError unless the counter is an integer the mode encodes exactly. */ setCounter(n) { if (!isValidCounter(n, this.mode)) { throw new RangeError( `counter must be an integer from 0 to ${MAX_COUNTER[this.mode]}. Got: ${n}` ); } if (this.mode === "string") { return concatBuffers(this.nonce, this.encoder.encode(n.toString())); } this.dataView.setUint32(this.nonce.length, n, false); return this.buffer; } } function assertKeyPrefix(keyPrefix, keyLength) { if (typeof keyPrefix !== "string" || !/^[0-9a-fA-F]+$/.test(keyPrefix)) { throw new Error("keyPrefix must be a non-empty hex string."); } if (keyPrefix.length > keyLength * 2) { throw new Error( `keyPrefix (${keyPrefix.length} hex characters) must not be longer than the key (keyLength: ${keyLength} bytes).` ); } } async function solveChallenge(options) { const { challenge, controller, counterMode = "uint32", counterStart = 0, counterStep = 1, deriveKey: deriveKey2, timeout = 9e4 } = options; const { nonce, keyLength = 32, keyPrefix, salt } = challenge.parameters; assertKeyPrefix(keyPrefix, keyLength); const nonceBuf = hexToBuffer(nonce); const saltBuf = hexToBuffer(salt); const keyPrefixHex = keyPrefix.toLowerCase(); const keyPrefixBuf = keyPrefix.length % 2 === 0 ? hexToBuffer(keyPrefix) : null; const password = new PasswordBuffer(nonceBuf, counterMode); const start = performance.now(); let counter = counterStart; let iterations = 0; let derivedKeyHex = ""; let lastYield = start; while (true) { if (controller?.signal.aborted || timeout && iterations % 10 === 0 && performance.now() - start > timeout) { return null; } const { derivedKey } = await deriveKey2( challenge.parameters, saltBuf, password.setCounter(counter) ); if (iterations % 10 === 0 && performance.now() - lastYield > 200) { await delay(0); lastYield = performance.now(); } if (keyPrefixBuf ? bufferStartsWith(derivedKey, keyPrefixBuf) : bufferToHex(derivedKey).startsWith(keyPrefixHex)) { derivedKeyHex = bufferToHex(derivedKey); break; } counter = counter + counterStep; iterations = iterations + 1; } return { counter, derivedKey: derivedKeyHex, time: timeDuration(start) }; } function handler(options) { const { deriveKey: deriveKey2 } = options; let controller = void 0; self.onmessage = async (message) => { const { challenge, counterMode, counterStart, counterStep, timeout, type } = message.data; if (type === "abort") { controller?.abort(); } else if (type === "work") { controller = new AbortController(); let solution; try { solution = await solveChallenge({ challenge, controller, counterStart, counterStep, deriveKey: deriveKey2, counterMode, timeout }); } catch (err) { return self.postMessage({ error: err }); } self.postMessage(solution); } }; } async function deriveKey(parameters, salt, password) { const { algorithm, cost, keyLength = 32 } = parameters; assertAlgorithm(algorithm, ["PBKDF2/SHA-256", "PBKDF2/SHA-384", "PBKDF2/SHA-512"]); const passwordKey = await crypto.subtle.importKey( "raw", password, { name: "PBKDF2" }, false, ["deriveBits"] ); const derivedBits = await crypto.subtle.deriveBits( { name: "PBKDF2", salt, iterations: cost, hash: algorithm.slice("PBKDF2/".length) }, passwordKey, keyLength * 8 ); return { parameters: {}, derivedKey: new Uint8Array(derivedBits) }; } handler({ deriveKey }); })();