Current Location: Home> Latest Articles> How to Combine PHP's hash_update and hash_final Functions to Complete the Hashing Process?

How to Combine PHP's hash_update and hash_final Functions to Complete the Hashing Process?

gitbox 2025-06-15

1. Overview of Hash Functions

In PHP, hash functions are implemented through the hash() family of functions. The hash() function itself accepts two main parameters: the algorithm and the data. It immediately calculates the hash value of the data and returns the result. However, there are times when we need to process large data incrementally, especially when the data is too large to load into memory all at once. In such cases, the hash_update and hash_final functions come into play.

2. Function Explanation

  1. hash_update
    The hash_update function is used to send data (such as a file or string) into the hashing process in batches. It adds new data to the existing hash calculation. By calling hash_update multiple times, large chunks of data can be processed incrementally.

    Syntax:

    hash_update ( resource $context , string $data ) : bool  
    
    • $context: A hash context resource, which can be created using the hash_init function.

    • $data: The data to be added to the current hash calculation.

  2. hash_final
    The hash_final function is used to complete the hash calculation after all data has been updated using hash_update, and returns the final hash value.

    Syntax:

    hash_final ( resource $context , bool $raw_output = false ) : string  
    
    • $context: The hash context associated with hash_update.

    • $raw_output: If true, returns raw binary data; if false, returns the hash value in hexadecimal form.

3. How to Use hash_update and hash_final to Complete the Hash Calculation

Next, we'll go through a specific example to explain how to combine hash_update and hash_final to complete the hash calculation.

Example Code:

<?php  
// Create a SHA256 hash context  
$context = hash_init(&#039;sha256&#039;);  
<p>// Incrementally update data<br>
$data1 = "Hello, this is the first part of the data.";<br>
$data2 = " And this is the second part of the data.";<br>
$data3 = " Finally, here is the third part of the data.";</p>
<p>hash_update($context, $data1);  // Update with the first part of the data<br>
hash_update($context, $data2);  // Update with the second part of the data<br>
hash_update($context, $data3);  // Update with the third part of the data</p>
<p>// Complete the hash calculation and get the result<br>
$hash = hash_final($context);</p>
<p>// Output the final hash value<br>
echo "The computed hash is: " . $hash;<br>
?><br>

Code Explanation:

  1. Initialize the Hash Context
    Use hash_init('sha256') to create a hash context resource. Here, we choose the sha256 algorithm, but you can select other hashing algorithms such as md5 or sha1 based on your needs.

  2. Incrementally Update Data
    Use the hash_update function to send data into the hashing process in batches. In our example, the data is divided into three parts, and each part is added to the hash context with three separate hash_update calls.

  3. Complete the Hash Calculation and Output the Result
    Use hash_final to complete the hash calculation and return the final hash value. You can choose to return the hash value as raw binary data or as a hexadecimal string. By default, it returns a hexadecimal string.

Output:

The computed hash is: 1c7d76e24a56bc69b890f3f8720a2eaf6cf4bb4e3ff315153f0a5e089b131b3c  

4. Why Use hash_update and hash_final?

When processing large data, directly using the hash() function to calculate the hash value all at once may lead to memory issues, especially when dealing with large files. Therefore, using hash_update to incrementally update the hash calculation can effectively save memory and ensure that even very large data can have its hash calculated smoothly.

Additionally, hash_update and hash_final provide flexibility, allowing the hash value to be computed incrementally as data flows, which is especially important for real-time data processing or when reading files in chunks.

5. Use Cases

  • File Verification: When uploading or downloading large files, the hash value of the file can be calculated incrementally to ensure the file has not been corrupted during transmission.

  • Password Storage: When storing user passwords, hashing algorithms are often used to encrypt the password. Incrementally updating the password data improves security.

  • Data Stream Encryption: For real-time encryption of large data streams, using hash_update and hash_final for batch processing can improve performance and memory usage efficiency.

6. Conclusion

PHP's hash_update and hash_final functions are powerful tools for handling large data and incrementally calculating hash values. By updating the hash context incrementally, you can effectively manage memory and ensure the integrity and efficiency of the hash calculation. In practical development, using these two functions appropriately can help developers efficiently complete hash calculation tasks.