Current Location: Home> Latest Articles> How to get a hash digest of any data in PHP using hash_final?

How to get a hash digest of any data in PHP using hash_final?

gitbox 2025-05-27

In PHP, hash_final() is a very useful function that returns a completed hash digest. Usually when using streaming hash algorithms (such as hash_init() and hash_update() ), we will use hash_final() to get the final hash value. This article will introduce how to use the hash_final() function in PHP to obtain a hash digest of arbitrary data and demonstrate how to operate it.

1. What is a hash summary?

A hash digest is a hashing algorithm that converts the input data into a fixed-length string. This summary is usually used in scenarios such as data integrity verification, digital signatures, etc. In PHP, various hash algorithms (such as MD5, SHA1, SHA256, etc.) can be used to generate a hash digest.

2. Overview of hash_final() function

hash_final() is a built-in function in PHP that completes a hash calculation and returns the final hash value. It is usually used in conjunction with hash_init() and hash_update() .

  • grammar :

     string hash_final ( resource $context [, bool $raw_output = false ] )
    
  • Parameter description :

    • $context : hash context returned by hash_init() .

    • $raw_output : If true , return a hash digest in binary format, otherwise returns a hash digest in hexadecimal format (default is false ).

3. Basic steps to get a hash digest using hash_final()

Step 1: Initialize the hash context

We first use the hash_init() function to initialize a hash context. This function requires specifying a hashing algorithm (such as SHA256).

 $context = hash_init('sha256');

Step 2: Update hash data

We then use the hash_update() function to add the data to the hash context. You can call hash_update() multiple times to add more data.

 $data = "Hello, world!";
hash_update($context, $data);

Step 3: Get the hash summary

Finally, use the hash_final() function to get the final hash digest and end the hash calculation.

 $hash = hash_final($context);
echo "The hash value is: " . $hash;

4. Sample code

Here is a complete example showing how to use the hash_final() function to generate a SHA256 hash value of a string:

 <?php
// initializationSHA256Hash context
$context = hash_init('sha256');

// Update data
$data = "This is some data to hash.";
hash_update($context, $data);

// Get the final hash summary
$hash = hash_final($context);

// Output hash value
echo "The SHA256 hash of the data is: " . $hash;
?>

The output will be a 64-character hexadecimal string representing the SHA256 hash digest of the data.

5. Use raw_output parameter

If you want to get a hash digest in binary format instead of hexadecimal format, you can set the raw_output parameter to true . This may be more useful for some application scenarios, such as storing hash values ​​to the database.

 $binary_hash = hash_final($context, true);
echo "The raw hash value is: " . bin2hex($binary_hash);  // Output is a hexadecimal string

6. Practical application: URL hash generation

If you want to calculate the hash of a URL, hash_final() can also help you achieve it. We can use hash_init() in combination with hash_update() to get the hash value of a specific URL. Suppose we need to calculate the hash value of the URL, here is an example:

 <?php
// initializationSHA256Hash context
$context = hash_init('sha256');

// Suppose this is what we want to calculate the hashURL
$url = "https://gitbox.net/example/page";

// renewURLdata
hash_update($context, $url);

// Get hash summary
$hash = hash_final($context);

// Output hash value
echo "The hash of the URL is: " . $hash;
?>

In this example, we compute the SHA256 hash digest of the URL https://gitbox.net/example/page . When using the hash_final() function, it always returns a fixed-length hash regardless of the length or content of the URL.

7. Conclusion

hash_final() is a very powerful function in PHP, which can help us easily calculate the hash digest of arbitrary data. Whether it is file content, string, or URL, etc., we can use it to generate hash values. Mastering the usage of hash_final() will help you better understand and apply hash algorithms, and improve code security and data integrity verification capabilities.