Skip to content

Instantly share code, notes, and snippets.

@jk
Created March 16, 2021 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jk/fda84175cd6db0f39db84b5667f3b9dd to your computer and use it in GitHub Desktop.
Save jk/fda84175cd6db0f39db84b5667f3b9dd to your computer and use it in GitHub Desktop.
Sodium Test
<?php
if (sodium_crypto_aead_aes256gcm_is_available()) {
echo 'sodium_crypto_aead_aes256gcm_is_available is true'.PHP_EOL;
} else {
echo 'sodium_crypto_aead_aes256gcm_is_available is false'.PHP_EOL;
}
//echo 'Generated key with sodium_crypto_aead_aes256gcm_keygen is: ' . sodium_crypto_aead_aes256gcm_keygen() . PHP_EOL;
$msg = 'john.doe@example.com';
$nonce = 'blablablabla';
$nonce2 = 'blablablabl2';
$key = 'SODIUM_CRYPTO_AEAD_AES256GCM_KEY';
echo SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES . PHP_EOL;
echo SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES . PHP_EOL;
$cipher = sodium_crypto_aead_aes256gcm_encrypt($msg, null, $nonce, $key);
echo $cipher . PHP_EOL;
echo base64_encode($cipher) . PHP_EOL;
$decoded_cipher = base64_decode(base64_encode($cipher));
$plaintext = sodium_crypto_aead_aes256gcm_decrypt($decoded_cipher, null, $nonce, $key);
echo $plaintext . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment