wppaste
WordPress

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
Encrypt a file

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

Reads or writes the filesystem via fread().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
16–131

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • filesystemfilesystem accessfread()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.

WhenInstructionsCalls it makes
!is_string($plaintext)16fread()
$mlen113–117fread(), ::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) && !$mlen128–131fread(), ::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

PHPCompiledExecutedBranchesNotes
8.6-dev17316–1315
8.517316–1315
8.417316–1315
8.317316–1315
8.217316–1315
8.117316–13151 fewer instruction than PHP 7.4
7.417416–1315

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

Used by · 2

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.

  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/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.