ParagonIE_Sodium_Compat::memzero( string|null $var ): void
- Source
wp-includes/sodium_compat/src/Compat.php:3837
Wipes a string variable passed by reference using the native sodium_memzero() function when the sodium extension is present, otherwise falls back to the old \Sodium\memzero() polyfill. If neither is available it throws a SodiumException instead of silently doing nothing, since sodium_compat cannot securely erase memory in pure PHP. Use it right before a sensitive string (a key, nonce, or decrypted payload) goes out of scope, and pair it with unset() as extra insurance.
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
$varstring|null
Return value
void
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.
Zero out a sensitive string after using it
Pull a value out of post meta to stand in for a sensitive string, then try to wipe it once it has been used.
$secret = (string) get_post_meta( 2, 'price', true );
printf( 'Before: %s' . PHP_EOL, esc_html( $secret ) );
try {
ParagonIE_Sodium_Compat::memzero( $secret );
printf( 'After: %s' . PHP_EOL, esc_html( var_export( $secret, true ) ) );
} catch ( SodiumException $e ) {
printf( 'memzero() refused: %s' . PHP_EOL, esc_html( $e->getMessage() ) );
}The visible result depends on whether the server's PHP build has the sodium extension enabled; on builds without it and without the legacy Sodium extension the call throws.
Guard a call to memzero() so it never throws
Check which code path memzero() will take before handing it an API key you want cleared from memory.
$api_key = 'sk_test_51H0000000000000';
if ( ParagonIE_Sodium_Compat::useNewSodiumAPI() ) {
ParagonIE_Sodium_Compat::memzero( $api_key );
printf( 'Wiped via the native sodium extension. Value now: %s' . PHP_EOL, esc_html( var_export( $api_key, true ) ) );
} elseif ( ParagonIE_Sodium_Compat::use_fallback( 'memzero' ) ) {
ParagonIE_Sodium_Compat::memzero( $api_key );
printf( 'Wiped via the legacy Sodium extension.' . PHP_EOL );
} else {
unset( $api_key );
printf( 'No native wiping available, unset() the variable instead.' . PHP_EOL );
}Common problems and fixes · 4
- Why does memzero() throw a SodiumException instead of just clearing the string?
- Why does my variable still show its old value after calling memzero()?
- Why do I get a 'only variables should be passed by reference' error?
- Is calling memzero() the same as calling sodium_memzero() directly?
Why does memzero() throw a SodiumException instead of just clearing the string?
Why does my variable still show its old value after calling memzero()?
Why do I get a 'only variables should be passed by reference' error?
Is calling memzero() the same as calling sodium_memzero() directly?
Alternatives and related functions
sodium_memzero- When you already know the sodium PHP extension is loaded and don't need the compat layer's fallback or exception handling.
ParagonIE_Sodium_Compat::useNewSodiumAPI- When you want to detect ahead of time whether memzero() will use the native extension instead of catching an exception after the fact.
ParagonIE_Sodium_Compat::use_fallback- When you need to know whether the legacy \Sodium\ extension fallback path is available before calling memzero().
SodiumException- When you need to catch or inspect the specific exception memzero() throws instead of a generic Exception.
Performance profile
How much work a call to ParagonIE_Sodium_Compat::memzero() 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
- 13–22
- Plugin surface
- None
- Called by
- 39
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 27.
Nothing here hands control to plugin code.
39 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost ParagonIE_Sodium_Compat::memzero() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
::useNewSodiumAPI() | 13 | ::ParagonIE_Sodium_Core_Util(), ::useNewSodiumAPI(), sodium_memzero() |
!::useNewSodiumAPI() && !::use_fallback() | 17 | ::ParagonIE_Sodium_Core_Util(), ::useNewSodiumAPI(), ::use_fallback() |
!::useNewSodiumAPI() && ::use_fallback() | 19–22 | ::ParagonIE_Sodium_Core_Util(), ::useNewSodiumAPI(), ::use_fallback(), Sodium\\memzero() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 27 instructions, 13–22 executed per call, 3 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.
Uses · 5
- sodium_memzero()
- ParagonIE_Sodium_Core_Util::declareScalarType()If a variable does not match a given type, throw a TypeError.
- ParagonIE_Sodium_Compat::useNewSodiumAPI()Libsodium as implemented in PHP 7.2 and/or ext/sodium (via PECL)
- ParagonIE_Sodium_Compat::use_fallback()Should we use the libsodium core function instead? This is always a good idea, if it's available. (Unless we're in the middle of running our unit test suite.)
- SodiumException::__construct()
Used by · 39
- ParagonIE_Sodium_Compat::crypto_generichash_final()Get the final BLAKE2b hash output for a given context.
- ParagonIE_Sodium_Core32_Ed25519::sign_detached()
- ParagonIE_Sodium_Core32_Salsa20::salsa20()
- ParagonIE_Sodium_Core32_Salsa20::salsa20_xor_ic()
- ParagonIE_Sodium_Core_AEGIS128L::decrypt()
- ParagonIE_Sodium_Core_AEGIS256::decrypt()
- ParagonIE_Sodium_Core_Ed25519::sign_detached()
- ParagonIE_Sodium_Core_Salsa20::salsa20()
- ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic()
- ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_decrypt()AEAD Decryption with ChaCha20-Poly1305
- ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_encrypt()AEAD Encryption with ChaCha20-Poly1305
- ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_decrypt()AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
Show all 39
- ParagonIE_Sodium_Crypto32::aead_chacha20poly1305_ietf_encrypt()AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
- ParagonIE_Sodium_Crypto32::box_seal()X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair.
- ParagonIE_Sodium_Crypto32::box_seal_open()Opens a message encrypted via box_seal().
- ParagonIE_Sodium_Crypto32::secretbox()XSalsa20-Poly1305 authenticated symmetric-key encryption.
- ParagonIE_Sodium_Crypto32::secretbox_open()Decrypt a ciphertext generated via secretbox().
- ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305()XChaCha20-Poly1305 authenticated symmetric-key encryption.
- ParagonIE_Sodium_Crypto32::secretbox_xchacha20poly1305_open()Decrypt a ciphertext generated via secretbox_xchacha20poly1305().
- ParagonIE_Sodium_Crypto::aead_chacha20poly1305_decrypt()AEAD Decryption with ChaCha20-Poly1305
- ParagonIE_Sodium_Crypto::aead_chacha20poly1305_encrypt()AEAD Encryption with ChaCha20-Poly1305
- ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_decrypt()AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
- ParagonIE_Sodium_Crypto::aead_chacha20poly1305_ietf_encrypt()AEAD Encryption with ChaCha20-Poly1305, IETF mode (96-bit nonce)
- ParagonIE_Sodium_Crypto::box_seal()X25519-XSalsa20-Poly1305 with one ephemeral X25519 keypair.
- ParagonIE_Sodium_Crypto::box_seal_open()Opens a message encrypted via box_seal().
- ParagonIE_Sodium_Crypto::secretbox()XSalsa20-Poly1305 authenticated symmetric-key encryption.
- ParagonIE_Sodium_Crypto::secretbox_open()Decrypt a ciphertext generated via secretbox().
- ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305()XChaCha20-Poly1305 authenticated symmetric-key encryption.
- ParagonIE_Sodium_Crypto::secretbox_xchacha20poly1305_open()Decrypt a ciphertext generated via secretbox_xchacha20poly1305().
- ParagonIE_Sodium_File::box_open()Open a boxed file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_box_open(), but produces the same result.
- ParagonIE_Sodium_File::box_seal()Seal a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_box_seal(), but produces the same result.
- ParagonIE_Sodium_File::box_seal_open()Open a sealed file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_box_seal_open(), but produces the same result.
- ParagonIE_Sodium_File::secretbox_encrypt()Encrypt a file
- ParagonIE_Sodium_File::secretbox_encrypt_core32()Encrypt a file (32-bit)
- ParagonIE_Sodium_File::secretbox_open()Seal a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_secretbox_open(), but produces the same result.
- ParagonIE_Sodium_File::sign()Sign a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces the same result.
- ParagonIE_Sodium_File::sign_core32()Sign a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces the same result. (32-bit)
- memzero()
- sodium_memzero()
Source code
public static function memzero( #[\SensitiveParameter] &$var ) { /* Type checks: */ ParagonIE_Sodium_Core_Util::declareScalarType($var, 'string', 1); if (self::useNewSodiumAPI()) { /** @psalm-suppress MixedArgument */ sodium_memzero($var); return; } if (self::use_fallback('memzero')) { $func = '\\Sodium\\memzero'; $func($var); if ($var === null) { return; } } // This is the best we can do. throw new SodiumException( 'This is not implemented in sodium_compat, as it is not possible to securely wipe memory from PHP. ' . 'To fix this error, make sure libsodium is installed and the PHP extension is enabled.' ); }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.