Current Location: Home> Latest Articles> How to avoid potential time attack vulnerabilities when using hash_equals

How to avoid potential time attack vulnerabilities when using hash_equals

gitbox 2025-05-26

In PHP, the hash_equals function is widely used to safely compare two strings, especially to verify that the hash values ​​are consistent, thereby avoiding the time-leakage vulnerability caused by traditional string comparisons (Timing Attack). Time attack uses the time difference caused by different character matching during the comparison process to guess sensitive information. hash_equals effectively prevents such attacks through fixed time comparison.

However, using hash_equals correctly can truly ensure security. This article will introduce how to use hash_equals in real applications and avoid potential time attack vulnerabilities.


1. Why choose hash_equals?

Ordinary string comparisons such as == or == will return the result immediately when the first different character is encountered, which allows the attacker to gradually guess the correct content of the string by measuring the response time.

hash_equals ensures that the comparison process takes almost the same time regardless of the string content, and eliminates information leakage caused by time differences.


2. Examples of basic usage of hash_equals

 <?php
$known_hash = hash('sha256', 'secret_password');
$user_hash = $_POST['password_hash'];

if (hash_equals($known_hash, $user_hash)) {
    echo 'Verification is successful';
} else {
    echo 'Verification failed';
}
?>

In this example, hash_equals safely compares two hash values ​​to avoid time leakage caused by direct comparison with == .

Note: hash_equals requires that the two strings have the same length, otherwise false will be returned directly.


3. Key points to avoid potential time attacks

3.1 Ensure that the comparison string length is equal

If the two strings have different lengths, hash_equals will immediately return false , which may expose length information. Although length information generally does not cause serious security problems, length leakage may also be exploited in extreme environments.

Solution:

  • Unified length output, such as using a fixed length hash value.

  • Avoid directly comparing plain text passwords, and you should always have the passwords and compare them after hashing.

3.2 Using a safe hashing algorithm

Choose a strong hash algorithm to generate hash values, such as sha256 and sha512 , and avoid using outdated or unsafe algorithms.

3.3 Do not use hash_equals to compare plain passwords

Always process sensitive information first through the hash function, and then compare it with hash_equals . Direct comparison of plain text passwords cannot prevent time attacks in any case.


4. Case Study: A Complete Demonstration of Preventing Time Attacks

 <?php
// Pre-stored password hash,Using security algorithms
$stored_hash = hash('sha256', 'user_password_secret');

// Input from the user
$user_input = $_POST['password'] ?? '';

// First calculate the hash input by the user
$user_hash = hash('sha256', $user_input);

// use hash_equals Make time safety comparison
if (hash_equals($stored_hash, $user_hash)) {
    echo 'Login successfully';
} else {
    echo 'Login failed';
}
?>

This example takes full advantage of the security features of hash_equals to avoid time attacks. Even if an attacker tries to guess the password multiple times, the correct password cannot be inferred from the response time.


5. Additional advice

  • Use special password hash functions such as password_hash() and password_verify() for the password verification process, which internally implement safer and more complex verification logic.

  • Use HTTPS in network transmission to prevent intermediaries from eavesdropping.

  • Regularly update and upgrade PHP versions to ensure the security and performance of built-in functions.


By correctly using hash_equals and combining secure hash policies, it can effectively prevent potential time attack vulnerabilities and ensure the security of the application.
Safety is nothing small, details determine success or failure. I hope this article can help you better understand and compare time safety in PHP.