(PHP 7 >= 7.2.0, PHP 8)
sodium_crypto_aead_chacha20poly1305_encrypt — Chiffre puis authentifie avec ChaCha20-Poly1305
$message,$additional_data,$nonce,$keyChiffre puis authentifie avec ChaCha20-Poly1305.
messageLe message en texte clair à chiffrer.
additional_dataDonnées additionnelles authentifiées. Cela est utilisé dans la vérification de l'étiquette d'authentification ajoutée au texte chiffré, mais il n'est pas chiffré ou stocké dans le texte chiffré.
nonceUn nombre qui doit être utilisé une seule fois, par message. 8 octets de long.
keyLa clé de chiffrement. 256 bits.
Renvoie le texte chiffré et l'étiquette d'authentification sous la forme d'une chaîne d'octets binaires bruts.
A flip/flop unit test to give you a sample:
<?php
use PHPUnit\Framework\TestCase;
class SodiumTest extends TestCase
{
public function testSodium()
{
// or 32 cryptographically secure bytes
// store the key securely with other secrets in your app
$key = sodium_crypto_aead_xchacha20poly1305_ietf_keygen();
// 8-bytes nonce should be stored along with the ciphertext (will be needed for decryption)
// It is not sensitive, you may just prepend it before the ciphertext.
$nonce = random_bytes(SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES);
$flip = 'Hello, world!';
$ciphertext = sodium_crypto_aead_chacha20poly1305_encrypt($flip, $nonce, $nonce, $key);
$flop = sodium_crypto_aead_chacha20poly1305_decrypt($ciphertext, $nonce, $nonce, $key);
$this->assertEquals($flip, $flop);
}
}
?>
Side note: the nonce is used twice in this test, but you can use a username, an identifier or whatever you like in `$additional_data`