array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'de', ), 'this' => array ( 0 => 'function.sodium-crypto-secretstream-xchacha20poly1305-init-push.php', 1 => 'sodium_crypto_secretstream_xchacha20poly1305_init_push', ), 'up' => array ( 0 => 'ref.sodium.php', 1 => 'Sodium Funktionen', ), 'prev' => array ( 0 => 'function.sodium-crypto-secretstream-xchacha20poly1305-init-pull.php', 1 => 'sodium_crypto_secretstream_xchacha20poly1305_init_pull', ), 'next' => array ( 0 => 'function.sodium-crypto-secretstream-xchacha20poly1305-keygen.php', 1 => 'sodium_crypto_secretstream_xchacha20poly1305_keygen', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/sodium/functions/sodium-crypto-secretstream-xchacha20poly1305-init-push.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 7 >= 7.2.0, PHP 8)
sodium_crypto_secretstream_xchacha20poly1305_init_push — Initialize a secretstream context for encryption
Initialize a secretstream context for encryption.
key
Cryptography key. See sodium_crypto_secretstream_xchacha20poly1305_keygen().
An array with two string values:
Beispiel #1 sodium_crypto_secretstream_xchacha20poly1305_init_push() example
<?php
function encrypt_file(string $inputFilePath, string $outputFilePath, string $key): void
{
[$state, $header] = sodium_crypto_secretstream_xchacha20poly1305_init_push($key);
$inputFile = fopen($inputFilePath, 'rb');
$outputFile = fopen($outputFilePath, 'wb');
// Write the header:
fwrite($outputFile, $header);
$inputFileSize = fstat($inputFile)['size'];
// Encrypt the file and write its contents to the output file:
for ($i = 0; $i < $inputFileSize; $i += 8175) {
$ptxt_chunk = fread($inputFile, 8175);
$ctxt_chunk = sodium_crypto_secretstream_xchacha20poly1305_push($state, $ptxt_chunk);
fwrite($outputFile, $ctxt_chunk);
}
sodium_memzero($state);
fclose($inputFile);
fclose($outputFile);
}
// sodium_crypto_secretstream_xchacha20poly1305_keygen()
$key = sodium_base642bin('MS0lzb7HC+thY6jY01pkTE/cwsQxnRq0/2L1eL4Hxn8=', SODIUM_BASE64_VARIANT_ORIGINAL);
file_put_contents('hello.txt', 'Hello world!');
encrypt_file('hello.txt', 'hello.txt.encrypted', $key);
var_dump(sodium_bin2hex(file_get_contents('hello.txt.encrypted')));
?>
Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
string(106) "971e33b255f0990ef3931caf761c59136efa77b434832f28ec719e3ff73f5aec38b3bba1574ab5b70a8844d8da36a668e802cfea2c"