/** * Konstantzeitvergleich für Auth-Tokens (Webhook-Secrets, API-Keys). * * `a !== b` leakt timing-Information; `crypto.timingSafeEqual` vergleicht in * O(n) immer alle Bytes. Längen-Mismatch wird vorab geprüft, weil * `timingSafeEqual` bei ungleicher Länge wirft. */ import { timingSafeEqual } from 'node:crypto'; export function constantTimeEqual(a: string | null | undefined, b: string | null | undefined): boolean { if (a == null || b == null) return false; const ab = Buffer.from(a, 'utf8'); const bb = Buffer.from(b, 'utf8'); if (ab.length !== bb.length) return false; return timingSafeEqual(ab, bb); }