require_once dirname(dirname(__FILE__)) . '/autoload.php';
* This file will monkey patch the pure-PHP implementation in place of the
* PECL functions and constants, but only if they do not already exist.
* Thus, the functions or constants just proxy to the appropriate
* ParagonIE_Sodium_Compat method or class constant, respectively.
'BASE64_VARIANT_ORIGINAL',
'BASE64_VARIANT_ORIGINAL_NO_PADDING',
'BASE64_VARIANT_URLSAFE',
'BASE64_VARIANT_URLSAFE_NO_PADDING',
'CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES',
'CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES',
'CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES',
'CRYPTO_AEAD_CHACHA20POLY1305_ABYTES',
'CRYPTO_AEAD_AES256GCM_KEYBYTES',
'CRYPTO_AEAD_AES256GCM_NSECBYTES',
'CRYPTO_AEAD_AES256GCM_NPUBBYTES',
'CRYPTO_AEAD_AES256GCM_ABYTES',
'CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES',
'CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES',
'CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES',
'CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES',
'CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES',
'CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES',
'CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES',
'CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES',
'CRYPTO_BOX_SECRETKEYBYTES',
'CRYPTO_BOX_PUBLICKEYBYTES',
'CRYPTO_BOX_KEYPAIRBYTES',
'CRYPTO_KDF_CONTEXTBYTES',
'CRYPTO_KX_KEYPAIRBYTES',
'CRYPTO_KX_PUBLICKEYBYTES',
'CRYPTO_KX_SECRETKEYBYTES',
'CRYPTO_KX_SESSIONKEYBYTES',
'CRYPTO_GENERICHASH_BYTES',
'CRYPTO_GENERICHASH_BYTES_MIN',
'CRYPTO_GENERICHASH_BYTES_MAX',
'CRYPTO_GENERICHASH_KEYBYTES',
'CRYPTO_GENERICHASH_KEYBYTES_MIN',
'CRYPTO_GENERICHASH_KEYBYTES_MAX',
'CRYPTO_PWHASH_SALTBYTES',
'CRYPTO_PWHASH_STRPREFIX',
'CRYPTO_PWHASH_ALG_ARGON2I13',
'CRYPTO_PWHASH_ALG_ARGON2ID13',
'CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE',
'CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE',
'CRYPTO_PWHASH_MEMLIMIT_MODERATE',
'CRYPTO_PWHASH_OPSLIMIT_MODERATE',
'CRYPTO_PWHASH_MEMLIMIT_SENSITIVE',
'CRYPTO_PWHASH_OPSLIMIT_SENSITIVE',
'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES',
'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX',
'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE',
'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE',
'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE',
'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE',
'CRYPTO_SCALARMULT_BYTES',
'CRYPTO_SCALARMULT_SCALARBYTES',
'CRYPTO_SHORTHASH_BYTES',
'CRYPTO_SHORTHASH_KEYBYTES',
'CRYPTO_SECRETBOX_KEYBYTES',
'CRYPTO_SECRETBOX_MACBYTES',
'CRYPTO_SECRETBOX_NONCEBYTES',
'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES',
'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES',
'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES',
'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH',
'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL',
'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY',
'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL',
'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX',
'CRYPTO_SIGN_PUBLICKEYBYTES',
'CRYPTO_SIGN_SECRETKEYBYTES',
'CRYPTO_SIGN_KEYPAIRBYTES',
'CRYPTO_STREAM_KEYBYTES',
'CRYPTO_STREAM_NONCEBYTES',
'CRYPTO_STREAM_XCHACHA20_KEYBYTES',
'CRYPTO_STREAM_XCHACHA20_NONCEBYTES',
if (!defined("SODIUM_$constant") && defined("ParagonIE_Sodium_Compat::$constant")) {
define("SODIUM_$constant", constant("ParagonIE_Sodium_Compat::$constant"));
if (!is_callable('sodium_add')) {
* @see ParagonIE_Sodium_Compat::add()
* @throws SodiumException
function sodium_add(&$val, $addv)
ParagonIE_Sodium_Compat::add($val, $addv);
if (!is_callable('sodium_base642bin')) {
* @see ParagonIE_Sodium_Compat::bin2base64()
* @throws SodiumException
function sodium_base642bin($string, $variant, $ignore ='')
return ParagonIE_Sodium_Compat::base642bin($string, $variant, $ignore);
if (!is_callable('sodium_bin2base64')) {
* @see ParagonIE_Sodium_Compat::bin2base64()
* @throws SodiumException
function sodium_bin2base64($string, $variant)
return ParagonIE_Sodium_Compat::bin2base64($string, $variant);
if (!is_callable('sodium_bin2hex')) {
* @see ParagonIE_Sodium_Compat::hex2bin()
* @throws SodiumException
function sodium_bin2hex($string)
return ParagonIE_Sodium_Compat::bin2hex($string);
if (!is_callable('sodium_compare')) {
* @see ParagonIE_Sodium_Compat::compare()
* @throws SodiumException
function sodium_compare($a, $b)
return ParagonIE_Sodium_Compat::compare($a, $b);
if (!is_callable('sodium_crypto_aead_aes256gcm_decrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt()
* @param string $assocData
function sodium_crypto_aead_aes256gcm_decrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt($message, $assocData, $nonce, $key);
} catch (Exception $ex) {
if (!is_callable('sodium_crypto_aead_aes256gcm_encrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt()
* @param string $assocData
* @throws SodiumException
function sodium_crypto_aead_aes256gcm_encrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt($message, $assocData, $nonce, $key);
if (!is_callable('sodium_crypto_aead_aes256gcm_is_available')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available()
function sodium_crypto_aead_aes256gcm_is_available()
return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available();
if (!is_callable('sodium_crypto_aead_chacha20poly1305_decrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt()
* @param string $assocData
function sodium_crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key);
} catch (Exception $ex) {
if (!is_callable('sodium_crypto_aead_chacha20poly1305_encrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt()
* @param string $assocData
* @throws SodiumException
function sodium_crypto_aead_chacha20poly1305_encrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt($message, $assocData, $nonce, $key);
if (!is_callable('sodium_crypto_aead_chacha20poly1305_keygen')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_keygen()
function sodium_crypto_aead_chacha20poly1305_keygen()
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_keygen();
if (!is_callable('sodium_crypto_aead_chacha20poly1305_ietf_decrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt()
* @param string $assocData
function sodium_crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key);
} catch (Exception $ex) {
if (!is_callable('sodium_crypto_aead_chacha20poly1305_ietf_encrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt()
* @param string $assocData
* @throws SodiumException
function sodium_crypto_aead_chacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key);
if (!is_callable('sodium_crypto_aead_chacha20poly1305_ietf_keygen')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_keygen()
function sodium_crypto_aead_chacha20poly1305_ietf_keygen()
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_keygen();
if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_decrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_decrypt()
* @param string $assocData
function sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key, true);
} catch (Exception $ex) {
if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_encrypt()
* @param string $assocData
* @throws SodiumException
function sodium_crypto_aead_xchacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key, true);
if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_keygen')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_keygen()
function sodium_crypto_aead_xchacha20poly1305_ietf_keygen()
return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_keygen();
if (!is_callable('sodium_crypto_auth')) {
* @see ParagonIE_Sodium_Compat::crypto_auth()
* @throws SodiumException
function sodium_crypto_auth($message, $key)
return ParagonIE_Sodium_Compat::crypto_auth($message, $key);
if (!is_callable('sodium_crypto_auth_keygen')) {
* @see ParagonIE_Sodium_Compat::crypto_auth_keygen()
function sodium_crypto_auth_keygen()
return ParagonIE_Sodium_Compat::crypto_auth_keygen();
if (!is_callable('sodium_crypto_auth_verify')) {
* @see ParagonIE_Sodium_Compat::crypto_auth_verify()
* @throws SodiumException
function sodium_crypto_auth_verify($mac, $message, $key)
return ParagonIE_Sodium_Compat::crypto_auth_verify($mac, $message, $key);
if (!is_callable('sodium_crypto_box')) {
* @see ParagonIE_Sodium_Compat::crypto_box()
* @throws SodiumException
function sodium_crypto_box($message, $nonce, $kp)
return ParagonIE_Sodium_Compat::crypto_box($message, $nonce, $kp);
if (!is_callable('sodium_crypto_box_keypair')) {
* @see ParagonIE_Sodium_Compat::crypto_box_keypair()
* @throws SodiumException
function sodium_crypto_box_keypair()
return ParagonIE_Sodium_Compat::crypto_box_keypair();
if (!is_callable('sodium_crypto_box_keypair_from_secretkey_and_publickey')) {
* @see ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey()
* @throws SodiumException
function sodium_crypto_box_keypair_from_secretkey_and_publickey($sk, $pk)
return ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey($sk, $pk);
if (!is_callable('sodium_crypto_box_open')) {
* @see ParagonIE_Sodium_Compat::crypto_box_open()
function sodium_crypto_box_open($message, $nonce, $kp)
return ParagonIE_Sodium_Compat::crypto_box_open($message, $nonce, $kp);
} catch (Exception $ex) {
if (!is_callable('sodium_crypto_box_publickey')) {
* @see ParagonIE_Sodium_Compat::crypto_box_publickey()
* @throws SodiumException
function sodium_crypto_box_publickey($keypair)
return ParagonIE_Sodium_Compat::crypto_box_publickey($keypair);
if (!is_callable('sodium_crypto_box_publickey_from_secretkey')) {
* @see ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey()
* @throws SodiumException
function sodium_crypto_box_publickey_from_secretkey($sk)
return ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey($sk);
if (!is_callable('sodium_crypto_box_seal')) {
* @see ParagonIE_Sodium_Compat::crypto_box_seal()
* @param string $publicKey
* @throws SodiumException