S
Seoffix.com
Sign In Get Pro Membership

PHP 8 Server-Side

PHP 8.2: Constant-Time Cryptographic Token Verification

Intermediate PHP 25 XP

Preventing Timing Attacks in Authentication

In standard string comparisons ($a === $b), PHP returns false as soon as the first non-matching byte is found. An attacker measuring latency with microsecond accuracy can determine which characters are correct one byte at a time.

PHP provides hash_equals() which compares strings in constant time regardless of where a mismatch occurs.

In this lesson, you will build a production-grade API authentication validator that safely parses Authorization: Bearer sfx_live_... headers.

Hints

  1. Use preg_match('/^Bearer\s+(sfx_live_\w+)$/i', $authHeader, $matches)
  2. Compute hash('sha256', $token)
  3. Compare with hash_equals($knownHash, $computedHash)

Your answer

This lesson is checked against the lesson rules. Your code is never run on our servers.

AI Code Explainer & Translator

Runs without any outside service.
 
Mark Complete & Earn XP
Earn 25 XP. Finish every lesson in the track to earn its certificate.
Sign in to earn XP