// The Salary Box: a salary-match function of a key only the service holds. No storage. // Web Crypto only, so it runs unchanged in a Cloudflare Worker, Node 20+, Deno or a browser. // // employer -> listing(listingId, ceiling, basis) -> { locator (public, goes in the posting), verifyKey (employer keeps) } // candidate -> bid(locator, minimum) -> ticket (handed to the employer with the application) // employer -> check(verifyKey, ticket) -> "below" | "above" | "invalid" (runs on the employer's side) // // Locator: "..". The key ID is a public fingerprint of the service's secret (16 hex digits), // published at /api/key, so anyone holding a locator can see which key made it, and a key change shows in every new // locator. The basis ("USD base salary per year") is readable, so the candidate sees what the number covers. Both // are bound into the seal as associated data, so neither can be swapped. The listing ID and the ceiling inside the // seal are readable only by the service. // Ticket: 16 random bytes and a 16-byte tag over (nonce, yes/no) under the listing's verifyKey. The answer is not // in the ticket; only a verifyKey holder can test which answer the tag matches. To anyone else it is 32 random- // looking bytes, so bidding again and again teaches the candidate nothing. // The employer's records are the state: revoking a posting or changing its ceiling is the employer deciding which // listing IDs it honours. A new ceiling is a new listing() call with a new listing ID. The verify key is derived // from the listing ID, so the employer chooses IDs that only it can produce. const enc = new TextEncoder(), dec = new TextDecoder(); export const b64 = (b: Uint8Array) => btoa(String.fromCharCode(...b)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); export const unb64 = (s: string) => Uint8Array.from(atob(s.replace(/-/g, "+").replace(/_/g, "/")), (c) => c.charCodeAt(0)); const cat = (...parts: Uint8Array[]) => { const o = new Uint8Array(parts.reduce((n, p) => n + p.length, 0)); let i = 0; for (const p of parts) { o.set(p, i); i += p.length; } return o; }; const ID = /^[A-Za-z0-9._-]{1,64}$/; const BASIS = /^[\p{L}\p{N} .,:/()$€£%+-]{1,80}$/u; async function hmac(key: Uint8Array, data: Uint8Array) { const k = await crypto.subtle.importKey("raw", key, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]); return new Uint8Array(await crypto.subtle.sign("HMAC", k, data)); } // Constant-time comparison, so checking a ticket doesn't leak through timing. const same = (a: Uint8Array, b: Uint8Array) => a.length === b.length && a.reduce((d, x, i) => d | (x ^ b[i]), 0) === 0; const amount = (n: number) => { if (!Number.isSafeInteger(n) || n <= 0) throw new Error("amount must be a positive integer"); return n; }; // The readable parts of a locator. No key needed. export const keyIdOf = (locator: string) => locator.split(".")[0]; export const basisOf = (locator: string) => dec.decode(unb64(locator.split(".")[1])); const hex = (b: Uint8Array) => Array.from(b, (x) => x.toString(16).padStart(2, "0")).join(""); export class Gate { private secret: Uint8Array; constructor(secret: Uint8Array) { if (secret.length !== 32) throw new Error("secret must be 32 bytes"); this.secret = secret; } // One secret; every purpose gets its own key, so a blob made for one step can't stand in for another. private sub = (purpose: string) => hmac(this.secret, enc.encode(`salarybox v1 ${purpose}`)); private verifyKeyFor = async (listingId: string) => hmac(await this.sub("ticket"), enc.encode(listingId)); private aes = async (use: "encrypt" | "decrypt") => crypto.subtle.importKey("raw", await this.sub("locator"), "AES-GCM", false, [use]); // A public fingerprint of the secret. It reveals nothing about the secret, and it changes if the secret does. async keyId() { return hex((await this.sub("key id")).slice(0, 8)); } async listing(listingId: string, ceiling: number, basis: string) { if (!ID.test(listingId)) throw new Error("listingId: 1-64 of A-Z a-z 0-9 . _ -"); if (!BASIS.test(basis)) throw new Error("basis: up to 80 plain characters"); const kid = await this.keyId(), bb = b64(enc.encode(basis)), iv = crypto.getRandomValues(new Uint8Array(12)); const ct = new Uint8Array(await crypto.subtle.encrypt({ name: "AES-GCM", iv, additionalData: enc.encode(`${kid}.${bb}`) }, await this.aes("encrypt"), enc.encode(JSON.stringify([listingId, amount(ceiling)])))); return { locator: `${kid}.${bb}.${b64(cat(iv, ct))}`, verifyKey: b64(await this.verifyKeyFor(listingId)) }; } async bid(locator: string, minimum: number) { amount(minimum); const [kid, bb, sealed, extra] = locator.split("."); if (!kid || !bb || !sealed || extra !== undefined) throw new Error("bad locator"); if (kid !== await this.keyId()) throw new Error("locator made under a different key"); const raw = unb64(sealed); const pt = await crypto.subtle.decrypt({ name: "AES-GCM", iv: raw.slice(0, 12), additionalData: enc.encode(`${kid}.${bb}`) }, await this.aes("decrypt"), raw.slice(12)); // throws if forged or altered const [listingId, ceiling] = JSON.parse(dec.decode(pt)) as [string, number]; const nonce = crypto.getRandomValues(new Uint8Array(16)); const tag = (await hmac(await this.verifyKeyFor(listingId), cat(nonce, Uint8Array.of(minimum <= ceiling ? 1 : 0)))).slice(0, 16); return b64(cat(nonce, tag)); // the minimum is gone when this returns } } export async function check(verifyKey: string, ticket: string): Promise<"below" | "above" | "invalid"> { let raw: Uint8Array, k: Uint8Array; try { raw = unb64(ticket); k = unb64(verifyKey); } catch { return "invalid"; } if (raw.length !== 32 || k.length !== 32) return "invalid"; const nonce = raw.slice(0, 16), tag = raw.slice(16); const tagFor = async (bit: number) => (await hmac(k, cat(nonce, Uint8Array.of(bit)))).slice(0, 16); if (same(tag, await tagFor(1))) return "below"; // at or below the ceiling if (same(tag, await tagFor(0))) return "above"; return "invalid"; }