Tutorials Courses Interview Prep (100 Q&A) AI Tools (50) Developer Tools (50) Tracks Pricing Plans Blog & Articles Contact Us
Modern PHP 8.x & Internals 8-10 Years (Tech Lead) Hard Production Scenario

How do you implement and verify Cryptographic Signatures (HMAC-SHA256) with timing attack prevention in PHP?

Real-World Scenario / Incident Description

An attacker forged webhook authentication payloads because the signature check used standard `==` string comparison instead of `hash_equals()`.

718 views • Updated 1 month ago

Timing Attacks & Constant-Time String Comparison

Standard string comparisons (===) fail fast at the first differing byte. An attacker measuring millisecond response timing can deduce character by character.

$computedSignature = hash_hmac('sha256', $rawPayload, $secretKey);

// VULNERABLE to timing attacks:
// if ($computedSignature === $incomingSignature) { ... }

// SECURE constant-time verification:
if (! hash_equals($computedSignature, $incomingSignature)) {
    throw new SecurityException("Invalid webhook signature.");
}
Related Concepts: #Security #Cryptography #Timing Attack #HMAC #hash_equals