In PHP, sodium_crypto_secretstream_xchacha20poly1305_init_push() is useful when your application needs a specific operation rather than manually reproducing it. Creates an encryption state and header for an authenticated encrypted message stream. Store or transmit the header alongside the ciphertext.
Security context: The examples show API usage, not a complete secure messaging protocol. Keep encryption keys secret and use authenticated encryption when integrity is required.
Stream functions work with PHP stream resources, wrappers, contexts or custom filters. Not every wrapper supports every operation.
What Is sodium_crypto_secretstream_xchacha20poly1305_init_push() in PHP?
Creates an encryption state and header for an authenticated encrypted message stream. Store or transmit the header alongside the ciphertext.
Signature:
sodium_crypto_secretstream_xchacha20poly1305_init_push(string $key): array
Parameters:
$key— Secret symmetric key or lookup key, depending on the function; never log cryptographic keys.
Return value: array.
Example 1: Basic Usage
This first example shows the method or function in a minimal context. For environment-dependent operations, adapt the required path, stream or connection before running it.
<?php
$key = sodium_crypto_secretstream_xchacha20poly1305_keygen();
[$pushState, $header] = sodium_crypto_secretstream_xchacha20poly1305_init_push($key);
$encrypted = sodium_crypto_secretstream_xchacha20poly1305_push($pushState, "Report", "", SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL);
$pullState = sodium_crypto_secretstream_xchacha20poly1305_init_pull($header, $key);
$decoded = sodium_crypto_secretstream_xchacha20poly1305_pull($pullState, $encrypted);
var_dump($decoded[0] === "Report");
The important operation is sodium_crypto_secretstream_xchacha20poly1305_init_push(). Creates an encryption state and header for an authenticated encrypted message stream.
Example 2: Another Input or Scenario
The next example changes the input or surrounding context to show how the operation behaves in a second situation.
<?php
$key = sodium_crypto_secretstream_xchacha20poly1305_keygen();
[$pushState, $header] = sodium_crypto_secretstream_xchacha20poly1305_init_push($key);
$encrypted = sodium_crypto_secretstream_xchacha20poly1305_push($pushState, "Backup entry", "", SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL);
$pullState = sodium_crypto_secretstream_xchacha20poly1305_init_pull($header, $key);
$decoded = sodium_crypto_secretstream_xchacha20poly1305_pull($pullState, $encrypted);
var_dump($decoded[0] === "Backup entry");
Compare the inputs and the handling of the return value with the first example. Avoid assuming that filesystem paths, resource states or binary bytes are interchangeable.
Practical Example: Applying the Operation
This example illustrates how the operation fits into a small application workflow. Paths, streams and secrets must be created or provided securely by the calling application.
<?php
$key = sodium_crypto_secretstream_xchacha20poly1305_keygen();
[$encryptionState, $header] = sodium_crypto_secretstream_xchacha20poly1305_init_push($key);
$parts = ["First line", "Second line"];
$cipherChunks = [];
foreach ($parts as $partIndex => $part) {
$tag = $partIndex === count($parts) - 1 ? SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL : SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_MESSAGE;
$cipherChunks[] = sodium_crypto_secretstream_xchacha20poly1305_push($encryptionState, $part, "", $tag);
}
$decryptionState = sodium_crypto_secretstream_xchacha20poly1305_init_pull($header, $key);
foreach ($cipherChunks as $cipherChunk) {
$decoded = sodium_crypto_secretstream_xchacha20poly1305_pull($decryptionState, $cipherChunk);
if ($decoded === false) { throw new RuntimeException("Authentication failed"); }
echo $decoded[0], PHP_EOL;
}
Practical note: Creates an encryption state and header for an authenticated encrypted message stream. Store or transmit the header alongside the ciphertext.
Important Behavior and Edge Cases
- Creates an encryption state and header for an authenticated encrypted message stream. Store or transmit the header alongside the ciphertext.
- Check failure values and exceptions; do not assume every environment supports the same stream wrapper, extension or filesystem capability.
- When handling paths, streams or external input, validate inputs before using results and release resources after use.
Comparison With Related Functions
| Function or Method | Behavior |
|---|---|
sodium_crypto_secretstream_xchacha20poly1305_init_push() | uses authenticated chunked encryption with a header and tag. |
sodium_crypto_stream_xchacha20_xor() | uses unauthenticated raw stream encryption. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Assuming sodium_crypto_secretstream_xchacha20poly1305_init_push() can be used without its required setup | Provide the correct file, stream, state, key or object before calling it; Creates an encryption state and header for an authenticated encrypted message stream. |
| Forgetting to transmit the secretstream header | Send the public header with the encrypted chunks so the receiver can initialize its decryption state. |
| Changing the chunk order or skipping authentication checks | Process chunks sequentially, check the result of pull(), and finish with the FINAL tag. |
Key Takeaways
sodium_crypto_secretstream_xchacha20poly1305_init_push()creates an encryption state and header for an authenticated encrypted message stream. Store or transmit the header alongside the ciphertext.- Use appropriate input types and verify results before continuing.
- Prefer specialized, safer APIs when the operation concerns untrusted input, encryption or network traffic.
In summary: Creates an encryption state and header for an authenticated encrypted message stream. Store or transmit the header alongside the ciphertext. Build in error handling and use this function where its specific behavior matches the application requirements.