require_once dirname(dirname(__FILE__)) . '/autoload.php';
use ParagonIE_Sodium_Compat;
* This file will monkey patch the pure-PHP implementation in place of the
* PECL functions, but only if they do not already exist.
* Thus, the functions just proxy to the appropriate ParagonIE_Sodium_Compat
if (!is_callable('\\Sodium\\bin2hex')) {
* @see ParagonIE_Sodium_Compat::bin2hex()
* @throws \SodiumException
function bin2hex($string)
return ParagonIE_Sodium_Compat::bin2hex($string);
if (!is_callable('\\Sodium\\compare')) {
* @see ParagonIE_Sodium_Compat::compare()
* @throws \SodiumException
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 crypto_aead_aes256gcm_decrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt($message, $assocData, $nonce, $key);
} catch (\TypeError $ex) {
} catch (\SodiumException $ex) {
if (!is_callable('\\Sodium\\crypto_aead_aes256gcm_encrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt()
* @param string $assocData
* @throws \SodiumException
function 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 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 crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key);
} catch (\TypeError $ex) {
} catch (\SodiumException $ex) {
if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_encrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt()
* @param string $assocData
* @throws \SodiumException
function 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_ietf_decrypt')) {
* @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt()
* @param string $assocData
function crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key)
return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key);
} catch (\TypeError $ex) {
} catch (\SodiumException $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 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_auth')) {
* @see ParagonIE_Sodium_Compat::crypto_auth()
* @throws \SodiumException
function crypto_auth($message, $key)
return ParagonIE_Sodium_Compat::crypto_auth($message, $key);
if (!is_callable('\\Sodium\\crypto_auth_verify')) {
* @see ParagonIE_Sodium_Compat::crypto_auth_verify()
* @throws \SodiumException
function 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 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 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 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 crypto_box_open($message, $nonce, $kp)
return ParagonIE_Sodium_Compat::crypto_box_open($message, $nonce, $kp);
} catch (\TypeError $ex) {
} catch (\SodiumException $ex) {
if (!is_callable('\\Sodium\\crypto_box_publickey')) {
* @see ParagonIE_Sodium_Compat::crypto_box_publickey()
* @throws \SodiumException
function 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 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_open()
* @param string $publicKey
* @throws \SodiumException
function crypto_box_seal($message, $publicKey)
return ParagonIE_Sodium_Compat::crypto_box_seal($message, $publicKey);
if (!is_callable('\\Sodium\\crypto_box_seal_open')) {
* @see ParagonIE_Sodium_Compat::crypto_box_seal_open()
function crypto_box_seal_open($message, $kp)
return ParagonIE_Sodium_Compat::crypto_box_seal_open($message, $kp);
} catch (\TypeError $ex) {
} catch (\SodiumException $ex) {
if (!is_callable('\\Sodium\\crypto_box_secretkey')) {
* @see ParagonIE_Sodium_Compat::crypto_box_secretkey()
* @throws \SodiumException
function crypto_box_secretkey($keypair)
return ParagonIE_Sodium_Compat::crypto_box_secretkey($keypair);
if (!is_callable('\\Sodium\\crypto_generichash')) {
* @see ParagonIE_Sodium_Compat::crypto_generichash()
* @param string|null $key
* @throws \SodiumException
function crypto_generichash($message, $key = null, $outLen = 32)
return ParagonIE_Sodium_Compat::crypto_generichash($message, $key, $outLen);
if (!is_callable('\\Sodium\\crypto_generichash_final')) {
* @see ParagonIE_Sodium_Compat::crypto_generichash_final()
* @param string|null $ctx
* @param int $outputLength
* @throws \SodiumException
function crypto_generichash_final(&$ctx, $outputLength = 32)
return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength);
if (!is_callable('\\Sodium\\crypto_generichash_init')) {
* @see ParagonIE_Sodium_Compat::crypto_generichash_init()
* @param string|null $key
* @throws \SodiumException
function crypto_generichash_init($key = null, $outLen = 32)
return ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outLen);
if (!is_callable('\\Sodium\\crypto_generichash_update')) {
* @see ParagonIE_Sodium_Compat::crypto_generichash_update()
* @param string|null $ctx
* @throws \SodiumException
function crypto_generichash_update(&$ctx, $message = '')
ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $message);
if (!is_callable('\\Sodium\\crypto_kx')) {
* @see ParagonIE_Sodium_Compat::crypto_kx()
* @param string $my_secret
* @param string $their_public
* @param string $client_public
* @param string $server_public
* @throws \SodiumException
function crypto_kx($my_secret, $their_public, $client_public, $server_public)
return ParagonIE_Sodium_Compat::crypto_kx(
if (!is_callable('\\Sodium\\crypto_pwhash')) {
* @see ParagonIE_Sodium_Compat::crypto_pwhash()
* @throws \SodiumException
function crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit)
return ParagonIE_Sodium_Compat::crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit);
if (!is_callable('\\Sodium\\crypto_pwhash_str')) {
* @see ParagonIE_Sodium_Compat::crypto_pwhash_str()
* @throws \SodiumException
function crypto_pwhash_str($passwd, $opslimit, $memlimit)
return ParagonIE_Sodium_Compat::crypto_pwhash_str($passwd, $opslimit, $memlimit);
if (!is_callable('\\Sodium\\crypto_pwhash_str_verify')) {
* @see ParagonIE_Sodium_Compat::crypto_pwhash_str_verify()
* @throws \SodiumException
function crypto_pwhash_str_verify($passwd, $hash)
return ParagonIE_Sodium_Compat::crypto_pwhash_str_verify($passwd, $hash);
if (!is_callable('\\Sodium\\crypto_pwhash_scryptsalsa208sha256')) {
* @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256()
* @throws \SodiumException
function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit)
return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit);
if (!is_callable('\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str')) {
* @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str()
* @throws \SodiumException
function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit)
return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit);
if (!is_callable('\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str_verify')) {
* @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify()
* @throws \SodiumException
function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash)
return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash);
if (!is_callable('\\Sodium\\crypto_scalarmult')) {
* @see ParagonIE_Sodium_Compat::crypto_scalarmult()
* @throws \SodiumException
function crypto_scalarmult($n, $p)