ParagonIE_Sodium_Compat::use_fallback( string $sodium_func_name = '' ): bool
- Source
wp-includes/sodium_compat/src/Compat.php:4491
Checks whether ParagonIE_Sodium_Compat should hand off to the native libsodium extension instead of its bundled PHP implementation. It looks for the legacy PECL "libsodium" extension on PHP 5.3 or later, and forces the PHP fallback when the class's internal disableFallbackForUnitTests flag is set. Because it is protected, it is only ever called from inside the other ParagonIE_Sodium_Compat methods that WordPress relies on for signing, hashing and encryption. Passing a $sodium_func_name narrows the check further, confirming that specific function actually exists under the old \Sodium\ namespace before it is trusted.
Description
If ext/libsodium is available, use it. Return TRUE.
Otherwise, we have to use the code provided herein. Return FALSE.
Compatibility
- WordPress
- core
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$sodium_func_namestringoptional- Default:
''
Return value
bool
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Check whether WordPress is using the native libsodium extension or the PHP fallback
Reflection is needed because use_fallback() is a protected method on ParagonIE_Sodium_Compat.
$reflection = new ReflectionMethod( 'ParagonIE_Sodium_Compat', 'use_fallback' );
$reflection->setAccessible( true );
$uses_native = $reflection->invoke( null );
printf(
'WordPress will use the native libsodium extension: %s',
esc_html( $uses_native ? 'yes' : 'no' )
);In real plugin code you would not call use_fallback() at all; just call a public method like crypto_box() and let it decide internally.
Check whether a specific legacy Sodium function is callable
Passing a function name makes use_fallback() also confirm that exact \Sodium\ function exists before returning true.
$reflection = new ReflectionMethod( 'ParagonIE_Sodium_Compat', 'use_fallback' );
$reflection->setAccessible( true );
$function_name = 'crypto_box_seal';
$is_callable = $reflection->invoke( null, $function_name );
printf(
'\\Sodium\\%s() is callable on this server: %s',
esc_html( $function_name ),
esc_html( $is_callable ? 'yes' : 'no' )
);On almost every current PHP install this prints "no", since PHP 7.2+ ships sodium as ext-sodium rather than the old PECL libsodium extension.
Common problems and fixes · 4
- Why does use_fallback() always return false even though sodium works fine on my server?
- Why can't I call ParagonIE_Sodium_Compat::use_fallback() from my own plugin code?
- Why does the result change when I run my code inside PHPUnit but not in production?
- Does the $sodium_func_name argument make the check stricter or looser?
Why does use_fallback() always return false even though sodium works fine on my server?
Why can't I call ParagonIE_Sodium_Compat::use_fallback() from my own plugin code?
Why does the result change when I run my code inside PHPUnit but not in production?
Does the $sodium_func_name argument make the check stricter or looser?
Alternatives and related functions
ParagonIE_Sodium_Compat::crypto_box- When you actually need to perform authenticated public-key encryption, call this directly since it already invokes use_fallback() internally to pick the right implementation.
ParagonIE_Sodium_Compat::crypto_auth- When you just need to generate or verify a message authentication code, use this public method instead of probing use_fallback() yourself.
ParagonIE_Sodium_Compat::bin2hex- When you only need libsodium's constant-time binary-to-hex conversion, call this method directly rather than checking which backend will handle it.
Performance profile
How much work a call to ParagonIE_Sodium_Compat::use_fallback() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 7–21
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 24.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost ParagonIE_Sodium_Compat::use_fallback() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$res !== null | 7–11 | none |
$res === null | 12–17 | extension_loaded() |
$res !== null && $res !== false && !empty($sodium_func_name) | 15 | is_callable() |
$res === null && $res !== false && !empty($sodium_func_name) | 20–21 | extension_loaded(), is_callable() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 24 instructions, 7–21 executed per call, 5 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Used by · 50
- ParagonIE_Sodium_Compat::bin2hex()Cache-timing-safe implementation of bin2hex().
- ParagonIE_Sodium_Compat::compare()Compare two strings, in constant-time.
- ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available()Is AES-256-GCM even available to use?
- ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt()Authenticated Encryption with Associated Data: Decryption
- ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt()Authenticated Encryption with Associated Data
- ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt()Authenticated Encryption with Associated Data: Decryption
- ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt()Authenticated Encryption with Associated Data
- ParagonIE_Sodium_Compat::crypto_auth()Authenticate a message. Uses symmetric-key cryptography.
- ParagonIE_Sodium_Compat::crypto_auth_verify()Verify the MAC of a message previously authenticated with crypto_auth.
- ParagonIE_Sodium_Compat::crypto_box()Authenticated asymmetric-key encryption. Both the sender and recipient may decrypt messages.
- ParagonIE_Sodium_Compat::crypto_box_keypair()Generate a new random X25519 keypair.
- ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey()Combine two keys into a keypair for use in library methods that expect a keypair. This doesn't necessarily have to be the same person's keys.
Show all 50
- ParagonIE_Sodium_Compat::crypto_box_open()Decrypt a message previously encrypted with crypto_box().
- ParagonIE_Sodium_Compat::crypto_box_publickey()Extract the public key from a crypto_box keypair.
- ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey()Calculate the X25519 public key from a given X25519 secret key.
- ParagonIE_Sodium_Compat::crypto_box_seal()Anonymous public-key encryption. Only the recipient may decrypt messages.
- ParagonIE_Sodium_Compat::crypto_box_seal_open()Opens a message encrypted with crypto_box_seal(). Requires the recipient's keypair (sk || pk) to decrypt successfully.
- ParagonIE_Sodium_Compat::crypto_box_secretkey()Extract the secret key from a crypto_box keypair.
- ParagonIE_Sodium_Compat::crypto_box_seed_keypair()Generate an X25519 keypair from a seed.
- ParagonIE_Sodium_Compat::crypto_generichash()Calculates a BLAKE2b hash, with an optional key.
- ParagonIE_Sodium_Compat::crypto_generichash_final()Get the final BLAKE2b hash output for a given context.
- ParagonIE_Sodium_Compat::crypto_generichash_init()Initialize a BLAKE2b hashing context, for use in a streaming interface.
- ParagonIE_Sodium_Compat::crypto_generichash_update()Update a BLAKE2b hashing context with additional data.
- ParagonIE_Sodium_Compat::crypto_kx()Perform a key exchange, between a designated client and a server.
- ParagonIE_Sodium_Compat::crypto_pwhash()
- ParagonIE_Sodium_Compat::crypto_pwhash_is_available()!Exclusive to sodium_compat!
- ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256()
- ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_is_available()!Exclusive to sodium_compat!
- ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str()
- ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify()
- ParagonIE_Sodium_Compat::crypto_pwhash_str()
- ParagonIE_Sodium_Compat::crypto_pwhash_str_verify()
- ParagonIE_Sodium_Compat::crypto_scalarmult()Calculate the shared secret between your secret key and your recipient's public key.
- ParagonIE_Sodium_Compat::crypto_scalarmult_base()Calculate an X25519 public key from an X25519 secret key.
- ParagonIE_Sodium_Compat::crypto_secretbox()Authenticated symmetric-key encryption.
- ParagonIE_Sodium_Compat::crypto_secretbox_open()Decrypts a message previously encrypted with crypto_secretbox().
- ParagonIE_Sodium_Compat::crypto_shorthash()Calculates a SipHash-2-4 hash of a message for a given key.
- ParagonIE_Sodium_Compat::crypto_sign()Returns a signed message. You probably want crypto_sign_detached() instead, which only returns the signature.
- ParagonIE_Sodium_Compat::crypto_sign_detached()Calculate the Ed25519 signature of a message and return ONLY the signature.
- ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519()Convert an Ed25519 public key to a Curve25519 public key
- ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519()Convert an Ed25519 secret key to a Curve25519 secret key
- ParagonIE_Sodium_Compat::crypto_sign_keypair()Generate a new random Ed25519 keypair.
- ParagonIE_Sodium_Compat::crypto_sign_open()Validates a signed message then returns the message.
- ParagonIE_Sodium_Compat::crypto_sign_publickey()Extract an Ed25519 public key from an Ed25519 keypair.
- ParagonIE_Sodium_Compat::crypto_sign_publickey_from_secretkey()Calculate an Ed25519 public key from an Ed25519 secret key.
- ParagonIE_Sodium_Compat::crypto_sign_secretkey()Extract an Ed25519 secret key from an Ed25519 keypair.
- ParagonIE_Sodium_Compat::crypto_sign_seed_keypair()Generate an Ed25519 keypair from a seed.
- ParagonIE_Sodium_Compat::crypto_sign_verify_detached()Verify the Ed25519 signature of a message.
- ParagonIE_Sodium_Compat::crypto_stream()Expand a key and nonce into a keystream of pseudorandom bytes.
- ParagonIE_Sodium_Compat::crypto_stream_xor()DANGER! UNAUTHENTICATED ENCRYPTION!
Source code
protected static function use_fallback($sodium_func_name = '') { static $res = null; if ($res === null) { $res = extension_loaded('libsodium') && PHP_VERSION_ID >= 50300; } if ($res === false) { // No libsodium installed return false; } if (self::$disableFallbackForUnitTests) { // Don't fallback. Use the PHP implementation. return false; } if (!empty($sodium_func_name)) { return is_callable('\\Sodium\\' . $sodium_func_name); } return true; }Changelog
Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/sodium_compat/src/Compat.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.