ParagonIE_Sodium_File::secretbox_encrypt( resource $ifp, resource $ofp, int $mlen, string $nonce, string $key ): bool
- Source
wp-includes/sodium_compat/src/File.php:912
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
$ifpresource$ofpresource$mlenint$noncestring$keystring
Return value
bool
Performance profile
How much work a call to ParagonIE_Sodium_File::secretbox_encrypt() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 16–131
- Plugin surface
- None
- Called by
- 2
Reads or writes the filesystem via fread().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 173.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- filesystemfilesystem access
fread()called directly - sqldatabase query
->update()called directly
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_File::secretbox_encrypt() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_string($plaintext) | 16 | fread() |
$mlen | 113–117 | fread(), ::ftell(), ::ParagonIE_Sodium_Core_HSalsa20(), ::ParagonIE_Sodium_Core_Util(), str_repeat(), ::ParagonIE_Sodium_Core_Util(), ::ParagonIE_Sodium_Core_Salsa20(), ::ParagonIE_Sodium_Core_Util(), ::ftell(), str_repeat(), fwrite(), ::ParagonIE_Sodium_Core_Util(), ->update(), fwrite(), fseek(), fread() |
is_string($plaintext) && !$mlen | 128–131 | fread(), ::ftell(), ::ParagonIE_Sodium_Core_HSalsa20(), ::ParagonIE_Sodium_Core_Util(), str_repeat(), ::ParagonIE_Sodium_Core_Util(), ::ParagonIE_Sodium_Core_Salsa20(), ::ParagonIE_Sodium_Core_Util(), ::ftell(), str_repeat(), fwrite(), ::ParagonIE_Sodium_Core_Util(), ->update(), fwrite(), fseek(), ::ParagonIE_Sodium_Compat(), ::ParagonIE_Sodium_Compat(), ::ftell(), fseek(), ->finish(), fwrite(), fseek() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 173 | 16–131 | 5 | |
| 8.5 | 173 | 16–131 | 5 | |
| 8.4 | 173 | 16–131 | 5 | |
| 8.3 | 173 | 16–131 | 5 | |
| 8.2 | 173 | 16–131 | 5 | |
| 8.1 | 173 | 16–131 | 5 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 174 | 16–131 | 5 |
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 · 9
- ParagonIE_Sodium_File::secretbox_encrypt_core32()Encrypt a file (32-bit)
- SodiumException::__construct()
- ParagonIE_Sodium_File::ftell()
- ParagonIE_Sodium_Core_HSalsa20::hsalsa20()Calculate an hsalsa20 hash of a single block
- ParagonIE_Sodium_Core_Util::substr()Safe substring
- ParagonIE_Sodium_Core_Salsa20::salsa20_xor()
- ParagonIE_Sodium_Core_Poly1305_State::__construct()ParagonIE_Sodium_Core_Poly1305_State constructor.
- ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic()
- ParagonIE_Sodium_Compat::memzero()It's actually not possible to zero memory buffers in PHP. You need the native library for that.
Used by · 2
- ParagonIE_Sodium_File::box_encrypt()
- ParagonIE_Sodium_File::secretbox()Encrypt a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_secretbox(), but produces the same result.
Source code
protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) { if (PHP_INT_SIZE === 4) { return self::secretbox_encrypt_core32($ifp, $ofp, $mlen, $nonce, $key); } $plaintext = fread($ifp, 32); if (!is_string($plaintext)) { throw new SodiumException('Could not read input file'); } $first32 = self::ftell($ifp); /** @var string $subkey */ $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); /** @var string $realNonce */ $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); /** @var string $block0 */ $block0 = str_repeat("\x00", 32); /** @var int $mlen - Length of the plaintext message */ $mlen0 = $mlen; if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; } $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); /** @var string $block0 */ $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( $block0, $realNonce, $subkey ); $state = new ParagonIE_Sodium_Core_Poly1305_State( ParagonIE_Sodium_Core_Util::substr( $block0, 0, ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES ) ); // Pre-write 16 blank bytes for the Poly1305 tag $start = self::ftell($ofp); fwrite($ofp, str_repeat("\x00", 16)); /** @var string $c */ $cBlock = ParagonIE_Sodium_Core_Util::substr( $block0, ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES ); $state->update($cBlock); fwrite($ofp, $cBlock); $mlen -= 32; /** @var int $iter */ $iter = 1; /** @var int $incr */ $incr = self::BUFFER_SIZE >> 6; /* * Set the cursor to the end of the first half-block. All future bytes will * generated from salsa20_xor_ic, starting from 1 (second block). */ fseek($ifp, $first32, SEEK_SET); while ($mlen > 0) { $blockSize = $mlen > self::BUFFER_SIZE ? self::BUFFER_SIZE : $mlen; $plaintext = fread($ifp, $blockSize); if (!is_string($plaintext)) { throw new SodiumException('Could not read input file'); } $cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( $plaintext, $realNonce, $iter,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/File.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.