wppaste
WordPress

ParagonIE_Sodium_File::secretbox( string $inputFile, string $outputFile, string $nonce, string $key ): bool

Source
wp-includes/sodium_compat/src/File.php:465
Encrypt a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_secretbox(), but produces the same result.

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

$inputFilestring
Absolute path to a file on the filesystem
$outputFilestring
Absolute path to a file on the filesystem
$noncestring
Number to be used only once
$keystring
Encryption key

Return value

bool

Performance profile

How much work a call to ParagonIE_Sodium_File::secretbox() 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 file_exists().

Scaling
Constant

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

Instructions
13–64

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • filesystemfilesystem accessfile_exists()called directly

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost ParagonIE_Sodium_File::secretbox() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always13–17none
is_string($inputFile) && is_string($outputFile) && is_string($nonce)20–25::strlen()
is_string($inputFile) && is_string($outputFile) && is_string($nonce) && is_string($key)28::strlen(), ::strlen()
is_string($inputFile) && is_string($outputFile) && is_string($nonce) && is_string($key) && !file_exists()32::strlen(), ::strlen(), file_exists()
is_string($inputFile) && is_string($outputFile) && is_string($nonce) && is_string($key) && file_exists() && !is_int($size)37::strlen(), ::strlen(), file_exists(), filesize()
is_string($inputFile) && is_string($outputFile) && is_string($nonce) && is_string($key) && file_exists() && is_int($size) && !is_resource($ifp)46::strlen(), ::strlen(), file_exists(), filesize(), fopen()
is_string($inputFile) && is_string($outputFile) && is_string($nonce) && is_string($key) && file_exists() && is_int($size) && is_resource($ifp) && !is_resource($ofp)56::strlen(), ::strlen(), file_exists(), filesize(), fopen(), fopen(), fclose()
is_string($inputFile) && is_string($outputFile) && is_string($nonce) && is_string($key) && file_exists() && is_int($size) && is_resource($ifp) && is_resource($ofp)64::strlen(), ::strlen(), file_exists(), filesize(), fopen(), fopen(), ::secretbox_encrypt(), fclose(), fclose()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 119 instructions, 13–64 executed per call, 10 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 · 4

Source code

    public static function secretbox(        $inputFile,        $outputFile,        $nonce,        #[\SensitiveParameter]        $key    ) {        /* Type checks: */        if (!is_string($inputFile)) {            throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..');        }        if (!is_string($outputFile)) {            throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.');        }        if (!is_string($nonce)) {            throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.');        }         /* Input validation: */        if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) {            throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes');        }        if (!is_string($key)) {            throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.');        }        if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) {            throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes');        }         if (!file_exists($inputFile)) {            throw new SodiumException('Input file does not exist');        }        /** @var int $size */        $size = filesize($inputFile);        if (!is_int($size)) {            throw new SodiumException('Could not obtain the file size');        }         /** @var resource $ifp */        $ifp = @fopen($inputFile, 'rb');        if (!is_resource($ifp)) {            throw new SodiumException('Could not open input file for reading');        }         /** @var resource $ofp */        $ofp = fopen($outputFile, 'wb');        if (!is_resource($ofp)) {            fclose($ifp);            throw new SodiumException('Could not open output file for writing');        }         $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key);        fclose($ifp);        fclose($ofp);        return $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/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.