sodium_crypto_secretstream_xchacha20poly1305_init_push

(PHP 7 >= 7.2.0, PHP 8)

sodium_crypto_secretstream_xchacha20poly1305_init_pushInitialize a secretstream context for encryption

Description

sodium_crypto_secretstream_xchacha20poly1305_init_push(string $key): array

Initialize a secretstream context for encryption.

Parameters

key

Cryptography key. See sodium_crypto_secretstream_xchacha20poly1305_keygen().

Return Values

An array with two string values:

  • The secretstream state, needed for further pushes
  • The secretstream header, which needs to be provided to the recipient so they can pull data

Examples

Example #1 sodium_crypto_secretstream_xchacha20poly1305_init_push() example

<?php
function encrypt_file(string $inputFilePathstring $outputFilePathstring $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($inputFile8175);
        
$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')));
?>

The above example will output something similar to:

string(106) "971e33b255f0990ef3931caf761c59136efa77b434832f28ec719e3ff73f5aec38b3bba1574ab5b70a8844d8da36a668e802cfea2c"

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Was genau bedeutet "Vibe Coding"? Ein tiefgehender Blick für Entwickler

In der Welt der Softwareentwicklung gibt es unzählige Wege, wie man an ein Projekt herangeht. Manche schwören auf strikte Planung, andere auf bewährte Algorithmen und wieder andere lassen sich von etwas ganz anderem leiten: ihrem Gefühl. ...

admin

Autor : admin
Category: Software & Web-Development

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial