sodium_crypto_generichash_keygen generates a securely random secret key with the exact size required by its matching primitive.
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_keygen generates a securely random secret key with the exact size required by its matching primitive.
The sodium extension must be enabled.
Syntax and Return Value
sodium_crypto_generichash_keygen(): string
This API accepts no required parameters, or its signature must be checked on the target runtime.
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
$key = sodium_crypto_generichash_keygen();
echo strlen($key);
Example 2: A Different Use Case
Generate two independent keys and confirm the expected length and distinct random values.
<?php
// Check the alternative case.
$key = sodium_crypto_generichash_keygen();
$second = sodium_crypto_generichash_keygen();
var_dump(!hash_equals($key, $second));
echo strlen($key);
Practical Example: Checking the Result
Generate two independent keys and confirm the expected length and distinct random values.
<?php
// Verify the expected behavior before using results.
$key = sodium_crypto_generichash_keygen();
$second = sodium_crypto_generichash_keygen();
var_dump(strlen($second) === strlen($key) && !hash_equals($key, $second));
echo strlen($key);
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_keygen | sodium_crypto_generichash_keygen generates a securely random secret key with the exact size required by its matching primitive. |
| 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_keygen generates a securely random secret key with the exact size required by its matching primitive.
- 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.