sodium_crypto_kx_secretkey extracts the secret key from an encoded keypair.
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_kx_secretkey extracts the secret key from an encoded keypair.
The sodium extension must be enabled.
Syntax and Return Value
sodium_crypto_kx_secretkey(string $key_pair): string
| Parameter | Purpose |
|---|---|
| $key_pair (string) | Use the PHP signature above to determine the exact expected input type; validate untrusted inputs before calling. |
The declared return type is shown above. Errors can include false results or exceptions depending on this API: The roles are directional: client transmit matches server receive and vice versa; authenticate peer public keys.
Example 1: Basic Operation
A keypair alone does not authenticate a peer. Verify public-key identity in the protocol.
<?php
$keypair = sodium_crypto_kx_keypair();
$part = sodium_crypto_kx_secretkey($keypair);
var_dump(strlen($part) === SODIUM_CRYPTO_KX_SECRETKEYBYTES);
Example 2: A Different Use Case
Inspect keypair structure and size; never display private-key bytes.
<?php
// Check the alternative case.
$keypair = sodium_crypto_kx_keypair();
$part = sodium_crypto_kx_secretkey($keypair);
var_dump(is_string($part));
Practical Example: Checking the Result
Inspect keypair structure and size; never display private-key bytes.
<?php
// Verify the expected behavior before using results.
$keypair = sodium_crypto_kx_keypair();
$part = sodium_crypto_kx_secretkey($keypair);
var_dump(strlen($part) === 32);
Important Behavior and Edge Cases
The roles are directional: client transmit matches server receive and vice versa; authenticate peer public keys.
The roles are directional: client transmit matches server receive and vice versa; authenticate peer public keys.
Comparison with Related Functions
| Approach | Key distinction |
|---|---|
| sodium_crypto_kx_secretkey | sodium_crypto_kx_secretkey extracts the secret key from an encoded keypair. |
| Related technique | sodium_crypto_scalarmult yields a raw shared secret without directional session keys. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Confusing this operation with a neighboring cryptographic or security mechanism | sodium_crypto_scalarmult yields a raw shared secret without directional session keys. |
| Ignoring a security prerequisite or failure case | The roles are directional: client transmit matches server receive and vice versa; authenticate peer public keys. |
Key Takeaways
- sodium_crypto_kx_secretkey extracts the secret key from an encoded keypair.
- The roles are directional: client transmit matches server receive and vice versa; authenticate peer public keys.
- sodium_crypto_scalarmult yields a raw shared secret without directional session keys.
Apply this behavior to a real application only after confirming the required extension, configuration and failure handling on the deployment runtime.