Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save erickvasilev/3cc52ca58037b58b520afd071db60daa to your computer and use it in GitHub Desktop.
Save erickvasilev/3cc52ca58037b58b520afd071db60daa to your computer and use it in GitHub Desktop.
<?php
error_log(E_ALL);
require_once "sodium/autoload.php";
$password = "abc";
$message = 'this is danger';
$salt = random_bytes(SODIUM_CRYPTO_PWHASH_SALTBYTES);
$secret_key = sodium_crypto_pwhash(SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES,
$password, $salt, 3, 268435456, 2);
$block_size = 16;
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$padded_message = sodium_pad($message, $block_size);
$encrypted_message = sodium_crypto_secretbox($padded_message, $nonce, $secret_key);
$encrypted_message_save = base64_encode($encrypted_message);
$decrypted_padded_message = sodium_crypto_secretbox_open(base64_decode($encrypted_message_save), $nonce, $secret_key);
$decrypted_message = sodium_unpad($decrypted_padded_message, $block_size);
echo $encrypted_message_save;
echo "<br/>";
echo $decrypted_message ;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment