sodium_crypto_secretstream_xchacha20poly1305_init_pull

(PHP 7 >= 7.2.0, PHP 8)

sodium_crypto_secretstream_xchacha20poly1305_init_pullInitialize a secretstream context for decryption

Description

sodium_crypto_secretstream_xchacha20poly1305_init_pull(string $header, string $key): string

Initialize a secretstream context for decryption.

Parameters

header

The header of the secretstream. This should be one of the values produced by sodium_crypto_secretstream_xchacha20poly1305_init_push().

key

Encryption key (256-bit).

Return Values

Secretstream state.

Examples

Example #1 sodium_crypto_secretstream_xchacha20poly1305_init_pull() example

<?php
function decrypt_file(string $inputFilePathstring $outputFilePathstring $key): void
{
    
$inputFile fopen($inputFilePath'rb');
    
$outputFile fopen($outputFilePath'wb');
    
$header fread($inputFile24);

    
$state sodium_crypto_secretstream_xchacha20poly1305_init_pull($header$key);
    
$inputFileSize fstat($inputFile)['size'];

    
// Decrypt the file and write its contents to the output file:
    
for ($i 24$i $inputFileSize$i += 8192) {
        
$ctxt_chunk fread($inputFile8192);

        
// We're not using $tag, but in real protocols you can use this on encrypt to e.g.
        // trigger a re-key or indicate the end of file. Then, on decrypt, you can assert
        // this behavior.
        
[$ptxt_chunk$tag] = sodium_crypto_secretstream_xchacha20poly1305_pull($state$ctxt_chunk);
        
fwrite($outputFile$ptxt_chunk);
    }

    
sodium_memzero($state);
    
fclose($inputFile);
    
fclose($outputFile);
}

// sodium_crypto_secretstream_xchacha20poly1305_keygen()
$key sodium_base642bin('MS0lzb7HC+thY6jY01pkTE/cwsQxnRq0/2L1eL4Hxn8='SODIUM_BASE64_VARIANT_ORIGINAL);

$example sodium_hex2bin('971e33b255f0990ef3931caf761c59136efa77b434832f28ec719e3ff73f5aec38b3bba1574ab5b70a8844d8da36a668e802cfea2c');
file_put_contents('hello.enc'$example);
decrypt_file('hello.enc''hello.txt.decrypted'$key);
var_dump(file_get_contents('hello.txt.decrypted'));
?>

The above example will output something similar to:

string(12) "Hello world!"

Here you can write a comment


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

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

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-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