Content · Web Development · PHP

PHP sodium_crypto_generichash_final() Function: Usage and Examples

sodium_crypto_generichash_final finalizes and extracts the digest from incremental hash.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

sodium_crypto_generichash_final finalizes and extracts the digest from incremental hash state.

The examples explain the data format, expected results and the difference between this API and nearby cryptographic operations.

What It Does


The operation is intended for: sodium_crypto_generichash_final finalizes and extracts the digest from incremental hash state.

The sodium extension must be enabled.

Syntax and Return Value


sodium_crypto_generichash_final(string $state, int $length = ...): string
ParameterPurpose
$state (string)Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling.
$length (int)Requested output length; consult byte-versus-text units for the chosen API.

The declared return type is shown above. Errors can include false results or exceptions depending on this API: A non-keyed generic hash does not establish authenticity; key size and output length have limits.

Example 1: Basic Operation


A binary digest is displayed as hexadecimal text. A secret key makes this a keyed hash.

<?php
$message = "Account update";
$key = sodium_crypto_generichash_keygen();
$state = sodium_crypto_generichash_init($key);
sodium_crypto_generichash_update($state, $message);
$hash = sodium_crypto_generichash_final($state);
echo bin2hex($hash);

Example 2: A Different Use Case


A binary digest is displayed as hexadecimal text. A secret key makes this a keyed hash.

<?php
// Check the alternative case.
$message = "New order: 42";
$key = sodium_crypto_generichash_keygen();
$state = sodium_crypto_generichash_init($key);
sodium_crypto_generichash_update($state, $message);
$hash = sodium_crypto_generichash_final($state);
echo bin2hex($hash);

Practical Example: Checking the Result


A binary digest is displayed as hexadecimal text. A secret key makes this a keyed hash.

<?php
// Verify the expected behavior before using results.
$message = "Export completed";
$key = sodium_crypto_generichash_keygen();
$state = sodium_crypto_generichash_init($key);
sodium_crypto_generichash_update($state, $message);
$hash = sodium_crypto_generichash_final($state);
echo bin2hex($hash);

Important Behavior and Edge Cases


A non-keyed generic hash does not establish authenticity; key size and output length have limits.

A non-keyed generic hash does not establish authenticity; key size and output length have limits.

Comparison with Related Functions


ApproachKey distinction
sodium_crypto_generichash_finalsodium_crypto_generichash_final finalizes and extracts the digest from incremental hash state.
Related techniquesodium_crypto_auth provides a fixed-purpose keyed MAC; hash offers other digest algorithms.

Common Beginner Mistakes


MistakeCorrect approach
Confusing this operation with a neighboring cryptographic or security mechanismsodium_crypto_auth provides a fixed-purpose keyed MAC; hash offers other digest algorithms.
Ignoring a security prerequisite or failure caseA non-keyed generic hash does not establish authenticity; key size and output length have limits.

Key Takeaways


  • sodium_crypto_generichash_final finalizes and extracts the digest from incremental hash state.
  • A non-keyed generic hash does not establish authenticity; key size and output length have limits.
  • sodium_crypto_auth provides a fixed-purpose keyed MAC; hash offers other digest algorithms.

Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.

Share this resourceSend this page to someone who may find it useful.