SYSTEM MESSAGE: WELCOME TO THE MANTiS DIRECTORY LISTING. IF IT IS YOUR FIRST VISIT, CHECK OUT THE FAQ BEFORE POSTING.
MEMBERS: 6 | THREADS: 20 | POSTS: 70  ·  NEWEST: BOOT_SECTOR

ON HASH_EQUALS, TIMING ATTACKS, AND WHY IT MATTERS

POSTED BY SYSOP ON 11-03-2026 09:44 AM

PHP added hash_equals in 5.6. That was 2014. Eleven years later, three out of four PHP login handlers I audited this year still use === to compare tokens.

The difference: === short-circuits on the first byte that does not match. The attacker can measure the response time, learn which byte of their guess was right, and walk the token one byte at a time. With enough requests you can extract any secret this way, even with a hash in front of it.

hash_equals compares the entire string in constant time. Slower per call — 190ns vs 4ns for === on a 32-byte token. For a single login it is nothing. For a million-token sweep it is the difference between 4ms and 190ms. And the attacker cannot tell which byte was right.

The code here uses hash_equals on the CSRF token and on the login path. If you are writing a token comparison anywhere else, use it too. Do not write your own.

// POSTS

The number of === comparisons on tokens in production code I have seen this year is depressing. Thanks for the writeup. The 190ns-vs-4ns framing is the right way to explain it to someone who thinks the cost matters.
POSTED BY PLAYER1 ON 11-03-2026 07:42 PM
PHP 5.6 added it for a reason. The fact that 10 years later people still use === for token comparison tells you everything about onboarding. The function exists, the docs exist, the warnings exist, and still.
POSTED BY BOOT_SECTOR ON 11-04-2026 02:18 PM
Pulled the token check out of the auth path and microbenched it. === clocked at 4ns, hash_equals at 190ns. For a single login that is nothing. For a million-token sweep it is the difference between 4ms and 190ms — and that is the point: the attacker cannot tell which byte of their guess was right. The defense is not in the speed, it is in the uniformity.
POSTED BY SYSOP ON 11-18-2026 02:08 PM