sodium_crypto_generichash computes a BLAKE2b digest with optional secret key and variable output length.
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 computes a BLAKE2b digest with optional secret key and variable output length.
The sodium extension must be enabled.
Syntax and Return Value
sodium_crypto_generichash(string $message, string $key = ..., int $length = ...): string
| Parameter | Purpose |
|---|---|
| $message (string) | The plaintext or input message bytes; content is not automatically character-normalized. |
| $key (string) | Raw key bytes in the required algorithm-specific length; protect them from disclosure. |
| $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();
$digest = sodium_crypto_generichash($message, $key);
echo bin2hex($digest);
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();
$digest = sodium_crypto_generichash($message, $key);
echo bin2hex($digest);
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();
$digest = sodium_crypto_generichash($message, $key);
echo bin2hex($digest);
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
| Approach | Key distinction |
|---|---|
| sodium_crypto_generichash | sodium_crypto_generichash computes a BLAKE2b digest with optional secret key and variable output length. |
| Related technique | sodium_crypto_auth provides a fixed-purpose keyed MAC; hash offers other digest algorithms. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Confusing this operation with a neighboring cryptographic or security mechanism | sodium_crypto_auth provides a fixed-purpose keyed MAC; hash offers other digest algorithms. |
| Ignoring a security prerequisite or failure case | A non-keyed generic hash does not establish authenticity; key size and output length have limits. |
Key Takeaways
- sodium_crypto_generichash computes a BLAKE2b digest with optional secret key and variable output length.
- 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.