HMAC

Proves a piece of data was produced by whoever holds the key, and hasn't been altered since — without needing anywhere to store state. Used internally by Middlewares\Csrf's session-less token path and by SignedURL.

Methods

HMAC::sign(string $data, string $key, string $algorithm = 'sha256'): string   // hex-encoded digest
HMAC::verify(string $data, string $signature, string $key, string $algorithm = 'sha256'): bool // constant-time
$signature = HMAC::sign('payload-data', $secretKey);
HMAC::verify('payload-data', $signature, $secretKey);           // true
HMAC::verify('payload-data-tampered', $signature, $secretKey);  // false

verify() compares via ConstantTime::equals(), not ===, so a wrong-but-close guess doesn't fail measurably faster or slower than a wildly wrong one.

Next steps

  • SignedURL — built directly on this.
  • Csrf — the session-less token path.