PHP code example of petrknap / crypto-sodium
1. Go to this page and download the library: Download petrknap/crypto-sodium library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
petrknap / crypto-sodium example snippets
use PetrKnap\CryptoSodium\SecretBox;
$secretBox = new SecretBox();
$message = 'Hello World!';
$key = $secretBox->generateKey();
$ciphertext = $secretBox->encrypt($message, $key);
echo $secretBox->decrypt($ciphertext, $key);
$secretBox->eraseData($key);
use PetrKnap\CryptoSodium\Box;
$box = new Box();
$message = 'Hello World!';
$keyPair = $box->generateKeyPair();
$ciphertext = $box->encrypt($message, $keyPair);
echo $box->decrypt($ciphertext, $keyPair);
$box->eraseData($keyPair);
use PetrKnap\CryptoSodium\SecretStream\XChaCha20Poly1305;
$xChaCha20Poly1305 = new XChaCha20Poly1305();
$messageChunk1 = 'Hello ';
$messageChunk2 = 'World!';
$key = $xChaCha20Poly1305->generateKey();
$pushStream = $xChaCha20Poly1305->initPush($key);
$ciphertextHeader = $pushStream->header;
$ciphertextChunk1 = $pushStream->push($messageChunk1);
$ciphertextChunk2 = $pushStream->push($messageChunk2, tag: XChaCha20Poly1305::TAG_FINAL);
$pullStream = $xChaCha20Poly1305->initPull($ciphertextHeader, $key);
echo $pullStream->pull($ciphertextChunk1);
echo $pullStream->pull($ciphertextChunk2);
$xChaCha20Poly1305->eraseData($key);
use PetrKnap\CryptoSodium\Aead\Aes256Gcm;
$aes256Gcm = new Aes256Gcm();
$message = 'Hello World!';
$purpose = 'example';
$key = $aes256Gcm->generateKey();
$ciphertext = $aes256Gcm->encrypt($message, $key, additionalData: $purpose);
echo $aes256Gcm->decrypt($ciphertext, $key, additionalData: $purpose);
$aes256Gcm->eraseData($key);