Current Location: Home> Latest Articles> hash_final How to deal with different data formats when calculating hash values?

hash_final How to deal with different data formats when calculating hash values?

gitbox 2025-05-27

In PHP, the hash_final function is used to complete the last step of a hash context (created by hash_init ) and return the calculated hash value. It belongs to the hash extension of PHP and is usually used with hash_init , hash_update , hash_update_stream and hash_update_file .

So, when we use hash_final to calculate hash values, how will different data formats (such as strings, binary data, file streams, etc.) be handled? This article will be discussed in detail.

Basic usage of hash_final

Let's first look at a basic example:

 <?php
$context = hash_init('sha256');
hash_update($context, 'Hello World');
$hash = hash_final($context);
echo $hash;
?>

This code snippet will output the SHA-256 hash value of the string Hello World (in hexadecimal).

Notice:

  • The second parameter received by hash_update is a string.

  • No matter what type you provide (as long as it can be converted to a string), it will eventually be processed as a byte sequence.

How to process different data formats

1?? string

When you pass in a string (whether ASCII or UTF-8 encoding), PHP will process it directly in byte sequences.
For example:

 hash_update($context, "abc");  // What is dealing with '61 62 63' Three bytes

Note: Multi-byte characters (such as Chinese) are also calculated by entering hash according to their underlying byte sequence, rather than at the "character" granularity.

2?? Binary data

If you pass in binary data, for example, a binary stream obtained through file_get_contents , it is also directly fed by bytes:

 $data = file_get_contents('https://gitbox.net/image.png');
hash_update($context, $data);

The $data here may contain \0 (null byte), high byte, etc. PHP will not perform any filtering or escape, and will enter the hash calculation as it is.

3?? File Stream

If you want to calculate hash for large files, it is recommended to use hash_update_stream :

 $fp = fopen('https://gitbox.net/largefile.zip', 'rb');
$context = hash_init('sha256');
hash_update_stream($context, $fp);
$hash = hash_final($context);
fclose($fp);

Here, the file content is read in chunks and fed into the hash context to avoid loading the entire file into memory at once.

4?? File path

If you want to calculate hash on a file directly, you can use hash_file , but if you want to use hash_* function family, you need:

 $context = hash_init('sha256');
hash_update_file($context, 'https://gitbox.net/document.pdf');
$hash = hash_final($context);

hash_update_file automatically opens the file, reads its contents, and updates the context.

summary

  • hash_final just finishes the accumulated data.

  • Before the data enters the context ( hash_update , hash_update_stream , hash_update_file ) has been read in byte sequences , and does not care about the original "data type".

  • You need to make sure that the data you pass in is what you want to have (especially encoding or binary security issues).