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.
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.
<?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.
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.
Choose a strong hash algorithm to generate hash values, such as sha256 and sha512 , and avoid using outdated or unsafe algorithms.
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.
<?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.
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.