wppaste
WordPress

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.

Libsodium as implemented in PHP 7.2 and/or ext/sodium (via PECL)

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?

It is declared protected static, so PHP only allows the ParagonIE_Sodium_Compat class itself to invoke it. Fixes:
  • 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?

The source returns false unconditionally when self::$disableFallbackForUnitTests is true, overriding the PHP_VERSION_ID and extension_loaded('sodium') checks entirely. That flag exists for sodium_compat's own test suite, so check whether anything in your stack is setting it outside of tests.

Does it re-check the sodium extension on every encryption call?

No. The result of PHP_VERSION_ID >= 70000 && extension_loaded('sodium') is stored in a static $res variable the first time it runs and reused for every later call in that PHP process, so a change to loaded extensions won't be seen until a fresh request.

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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–8

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 9.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

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.

WhenInstructionsCalls it makes
always6–8none

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

Show all 50

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.