wppaste
WordPress

ParagonIE_Sodium_File::updateHashWithFile( resource|HashContext $hash, resource $fp, int $size = 0 ): resource|object

Source
wp-includes/sodium_compat/src/File.php:1166
Update a hash context with the contents of a file, without loading the entire file into memory.

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

$hashresource|HashContext
$fpresource
$sizeintoptional
Default: 0

Return value

resource|object
Resource on PHP < 7.2, HashContext object on PHP >= 7.2

Performance profile

How much work a call to ParagonIE_Sodium_File::updateHashWithFile() 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
12–39

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

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

What it touches

  • filesystemfilesystem accessfread()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::updateHashWithFile() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always12–16none
is_object($hash) && is_resource($fp) && is_int($size) && !$i28::ftell(), fseek(), fseek()
is_object($hash) && is_resource($fp) && is_int($size) && $i && !is_string($message)38–39::ftell(), fseek(), fread()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7312–396
8.57312–396
8.47312–396
8.37312–396
8.27312–396
8.17312–3962 fewer instructions than PHP 7.4
7.47512–406

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

Used by · 4

  • 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)
  • ParagonIE_Sodium_File::verify()Verify a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but produces the same result.
  • ParagonIE_Sodium_File::verify_core32()Verify a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but produces the same result. (32-bit)

Source code

    public static function updateHashWithFile($hash, $fp, $size = 0)    {        /* Type checks: */        if (PHP_VERSION_ID < 70200) {            if (!is_resource($hash)) {                throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.');            }        } else {            if (!is_object($hash)) {                throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.');            }        }         if (!is_resource($fp)) {            throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.');        }        if (!is_int($size)) {            throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.');        }         /** @var int $originalPosition */        $originalPosition = self::ftell($fp);         // Move file pointer to beginning of file        fseek($fp, 0, SEEK_SET);        for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) {            /** @var string|bool $message */            $message = fread(                $fp,                ($size - $i) > self::BUFFER_SIZE                    ? $size - $i                    : self::BUFFER_SIZE            );            if (!is_string($message)) {                throw new SodiumException('Unexpected error reading from file.');            }            /** @var string $message */            /** @psalm-suppress InvalidArgument */            self::hash_update($hash, $message);        }        // Reset file pointer's position        fseek($fp, $originalPosition, SEEK_SET);        return $hash;    }

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.