* Stable, deterministic hash of a spec. Uses SHA-256 over the canonicalized * JSON. Output is the first 12 hex chars — enough to avoid collision in * practice, short enough to fit in URLs.
(spec: ComputerSpec)
| 53 | * practice, short enough to fit in URLs. |
| 54 | */ |
| 55 | async function specHash(spec: ComputerSpec): Promise<string> { |
| 56 | const canonical = JSON.stringify(spec, Object.keys(spec).sort()); |
| 57 | const data = new TextEncoder().encode(canonical); |
| 58 | const digest = await crypto.subtle.digest("SHA-256", data); |
| 59 | const hex = Array.from(new Uint8Array(digest)) |
| 60 | .map((b) => b.toString(16).padStart(2, "0")) |
| 61 | .join(""); |
| 62 | return `cb_${hex.slice(0, 12)}`; |
| 63 | } |