* Derive a short, irreversible, truncated digest of a cache input. * * The output is deliberately far smaller than the entropy of a real API key: collisions * across the handful of keys/servers a single user configures are negligible (birthday bound * ~ n^2 / 2^(8*bytes)), while the truncated ou
(value: string, bytes: number)
| 97 | * cache filename cannot be reversed to identify the API key it was derived from. |
| 98 | */ |
| 99 | function deriveCacheDigest(value: string, bytes: number): string { |
| 100 | const memoKey = `${bytes}:${value}` |
| 101 | const cached = cacheDigestCache.get(memoKey) |
| 102 | if (cached) return cached |
| 103 | const digest = pbkdf2Sync(value, CACHE_DIGEST_SALT, CACHE_DIGEST_ITERATIONS, bytes, "sha256").toString("hex") |
| 104 | cacheDigestCache.set(memoKey, digest) |
| 105 | return digest |
| 106 | } |
| 107 | |
| 108 | // 4 bytes (8 hex chars) = 32 bits for the per-API-key discriminator embedded in the cache key. |
| 109 | const API_KEY_DISCRIMINATOR_BYTES = 4 |
no test coverage detected