ParagonIE_Sodium_Compat::useNewSodiumAPI(): bool
- Source
wp-includes/sodium_compat/src/Compat.php:4518
Decides at runtime whether WordPress's bundled sodium_compat library hands cryptographic calls off to PHP's native libsodium extension instead of its pure PHP fallback. It checks the PHP version and whether the sodium extension is loaded, then caches that answer in a static variable for the rest of the request. Because the method is protected, plugin and theme code cannot call it directly, only ParagonIE_Sodium_Compat's own methods (crypto_box, crypto_aead_chacha20poly1305_encrypt, bin2hex, and others) consult it internally to choose an implementation path.
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.
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's sodium compat layer is using the native extension
useNewSodiumAPI() is protected, so reflection is used here purely to inspect the decision it makes on the current server.
$reflection = new ReflectionMethod( 'ParagonIE_Sodium_Compat', 'useNewSodiumAPI' );
$reflection->setAccessible( true );
$uses_native_sodium = $reflection->invoke( null );
printf(
'PHP version: %s. sodium extension loaded: %s. sodium_compat will use the native extension: %s.',
esc_html( PHP_VERSION ),
esc_html( extension_loaded( 'sodium' ) ? 'yes' : 'no' ),
esc_html( $uses_native_sodium ? 'yes' : 'no' )
);In real plugin code you should not reflect into a protected method; this is only useful for diagnosing which crypto backend a host is running.
Hash a post's content with sodium_compat regardless of which backend it picks
Public sodium_compat methods like bin2hex() call useNewSodiumAPI() internally, so the caller gets the same result whether native libsodium or the PHP fallback runs.
$post = get_post( 2 );
$digest = hash( 'sha256', $post->post_content, true );
$hex = ParagonIE_Sodium_Compat::bin2hex( $digest );
printf(
'SHA-256 hash of post 2 content, encoded via sodium_compat::bin2hex(): %s',
esc_html( $hex )
);Common problems and fixes · 3
- Why can't I call ParagonIE_Sodium_Compat::useNewSodiumAPI() from my plugin?
- Why does it return false even though the server has PHP 7.2+ and the sodium extension loaded?
- Does it re-check the sodium extension on every encryption call?
Why can't I call ParagonIE_Sodium_Compat::useNewSodiumAPI() from my plugin?
- Call a public method instead, such as ParagonIE_Sodium_Compat::crypto_box() or ::bin2hex(), which use it internally
- Use ReflectionMethod::setAccessible(true) only for diagnostics, never in production request handling
Why does it return false even though the server has PHP 7.2+ and the sodium extension loaded?
Does it re-check the sodium extension on every encryption call?
Alternatives and related functions
extension_loaded- When you only need to know if the native sodium extension is present, without sodium_compat's static caching or its unit-test override flag.
function_exists- When you want to confirm a specific native function like sodium_crypto_secretbox exists before calling it directly instead of going through sodium_compat.
ParagonIE_Sodium_Compat::crypto_box- When you actually need to perform the encryption or decryption rather than inspect which backend will handle it.
Performance profile
How much work a call to ParagonIE_Sodium_Compat::useNewSodiumAPI() 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
- 6–8
- 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 9.
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 · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost ParagonIE_Sodium_Compat::useNewSodiumAPI() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–8 | none |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 9 instructions, 6–8 executed per call, 2 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_aead_xchacha20poly1305_ietf_decrypt()Authenticated Encryption with Associated Data: Decryption
- ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_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.
Show all 50
- 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.
- 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_keypair_from_secretkey_and_publickey()
- 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.
Source code
protected static function useNewSodiumAPI() { static $res = null; if ($res === null) { $res = PHP_VERSION_ID >= 70000 && extension_loaded('sodium'); } if (self::$disableFallbackForUnitTests) { // Don't fallback. Use the PHP implementation. return false; } return (bool) $res; }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.