Current Location: Home> Latest Articles> How to Use hash_copy and hash_equals to Compare Two Hash Values for Consistency?

How to Use hash_copy and hash_equals to Compare Two Hash Values for Consistency?

M66 2025-07-28

In PHP, hash functions are widely applied in scenarios such as password verification and data integrity checks. To ensure data security, it is often necessary to determine whether two hash values are identical. PHP offers multiple functions to generate and compare hash values, among which hash_copy and hash_equals are two commonly used functions that can effectively assist us in comparing hash values.

What Are hash_copy and hash_equals?

  1. hash_copy
    hash_copy is actually not a function that exists in PHP. If you have seen references to it in some documents or articles, it is likely a misuse or misunderstanding. Common PHP functions related to hash generation and comparison are hash() and hash_equals().

  2. hash_equals
    hash_equals is a function provided by PHP used to securely compare two hash values. Its purpose is to prevent timing attacks. With regular string comparison, attackers can infer differences in hash values by measuring the time taken during comparisons. hash_equals compares two hash strings character by character only if they have the same length, ensuring that timing differences are not exposed.

    bool hash_equals ( string $known_string , string $user_string )
    
    • $known_string: The known hash value or string.

    • $user_string: The user input hash value or string.

    • Return value: Returns true if the two strings are exactly the same, otherwise returns false.

How to Use hash_equals to Determine if Two Hash Values Are the Same?

When we want to compare whether two hash values are the same, we can use the hash_equals function to ensure a secure and accurate comparison. Here is a common example assuming we need to verify whether a user’s input password matches the hash stored in the database.

Example: Verifying User Input Password

<?php
// Assume the hash stored in the database
$stored_hash = '$2y$10$V5ldlhO.Jk19pZpWzchL7vJdknz57Vry8V15hATFmgt0gt04SKeQK'; // The hashed password
<p>// User input password<br>
$user_input_password = 'user_password';</p>
<p>// Generate hash using password_hash and verify<br>
$input_hash = password_hash($user_input_password, PASSWORD_DEFAULT);</p>
<p>// Use hash_equals to compare two hash values<br>
if (hash_equals($stored_hash, $input_hash)) {<br>
echo "Password matches!";<br>
} else {<br>
echo "Password does not match!";<br>
}<br>
?><br>

In this example, although password_hash hashes the user password, the role of hash_equals is to ensure the comparison does not introduce potential security risks due to differing lengths or timing differences.

Why Use hash_equals?

String comparison operations in PHP (such as == or strcmp()) carry certain security risks because their execution time depends on the length and content of the strings. For sensitive data like hash values, directly comparing with == allows attackers to infer differences by analyzing how long the comparison takes. This attack method is called a timing attack.

hash_equals is designed to prevent such attacks. It forces both strings to be the same length and compares them byte by byte, eliminating timing discrepancies that can expose security vulnerabilities.

Use Cases

  1. User Authentication
    When a user logs in, their input password is typically hashed and compared against the hash stored in the database. To ensure the verification process is secure, hash_equals should be used.

  2. Data Integrity Verification
    When transmitting data, a hash of the data can be calculated and sent along. The receiver can use hash_equals to compare the received hash with a locally computed one to verify the data has not been tampered with.

  3. Generating and Comparing File Hashes
    By hashing file contents, it is possible to detect if a file has changed. hash_equals ensures the comparison of file hashes is protected from external attacks.

Conclusion

hash_equals is a very secure and efficient hash comparison function in PHP that effectively prevents timing attacks. Although hash_copy is not a built-in PHP function, we can safely handle and compare hashes using functions like hash() and hash_equals. When verifying sensitive data, we should always use hash_equals to ensure the comparison process is secure and avoid potential security risks.