wppaste
WordPress

ParagonIE_Sodium_File::generichash( string $filePath, string|null $key = '', int $outputLength = 32 ): string

Source
wp-includes/sodium_compat/src/File.php:380
Calculate the BLAKE2b hash of 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

$filePathstring
Absolute path to a file on the filesystem
$keystring|nulloptional
BLAKE2b keyDefault: ''
$outputLengthintoptional
Length of hash outputDefault: 32

Return value

string
BLAKE2b hash

Performance profile

How much work a call to ParagonIE_Sodium_File::generichash() 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
Scales with input

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

Instructions
12–77

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

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 · 13 distinct outcomes

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

WhenInstructionsCalls it makes
always12–29none
is_string($filePath) && !empty($key)21–29::strlen()
is_string($filePath) && empty($key) && !$outputLength && !file_exists()25–33file_exists()
is_string($filePath) && !empty($key)27–41::strlen(), ::strlen()
is_string($filePath) && empty($key) && !$outputLength && file_exists() && !is_int($size)30–38file_exists(), filesize()
is_string($filePath) && empty($key) && !$outputLength && file_exists() && is_int($size) && !is_resource($fp)37–45file_exists(), filesize(), fopen()
is_string($filePath) && !empty($key) && !$outputLength && !file_exists()37–45::strlen(), ::strlen(), file_exists()
is_string($filePath) && !empty($key) && !$outputLength && file_exists() && !is_int($size)42–50::strlen(), ::strlen(), file_exists(), filesize()
is_string($filePath) && !empty($key) && !$outputLength && file_exists() && is_int($size) && !is_resource($fp)49–57::strlen(), ::strlen(), file_exists(), filesize(), fopen()
is_string($filePath) && empty($key) && !$outputLength && file_exists() && is_int($size) && is_resource($fp) && !$size49–57file_exists(), filesize(), fopen(), ::ParagonIE_Sodium_Compat(), fclose(), ::ParagonIE_Sodium_Compat()
is_string($filePath) && empty($key) && !$outputLength && file_exists() && is_int($size) && is_resource($fp) && $size && !is_string($read)56–65file_exists(), filesize(), fopen(), ::ParagonIE_Sodium_Compat(), fread()
is_string($filePath) && !empty($key) && !$outputLength && file_exists() && is_int($size) && is_resource($fp) && !$size61–69::strlen(), ::strlen(), file_exists(), filesize(), fopen(), ::ParagonIE_Sodium_Compat(), fclose(), ::ParagonIE_Sodium_Compat()
1 further outcome, up to 77 instructions
is_string($filePath) && !empty($key) && !$outputLength && file_exists() && is_int($size) && is_resource($fp) && $size && !is_string($read)68–77::strlen(), ::strlen(), file_exists(), filesize(), fopen(), ::ParagonIE_Sodium_Compat(), fread()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14012–7716
8.514012–7716
8.414012–77162 fewer instructions than PHP 8.3
8.314212–7916
8.214212–7916
8.114212–7916
7.414212–7916

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 · 6

Source code

    public static function generichash(        $filePath,        #[\SensitiveParameter]        $key = '',        $outputLength = 32    ) {        /* Type checks: */        if (!is_string($filePath)) {            throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');        }        if (!is_string($key)) {            if (is_null($key)) {                $key = '';            } else {                throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.');            }        }        if (!is_int($outputLength)) {            if (!is_numeric($outputLength)) {                throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.');            }            $outputLength = (int) $outputLength;        }         /* Input validation: */        if (!empty($key)) {            if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) {                throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes');            }            if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) {                throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes');            }        }        if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) {            throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN');        }        if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) {            throw new SodiumException('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX');        }         if (!file_exists($filePath)) {            throw new SodiumException('File does not exist');        }        /** @var int $size */        $size = filesize($filePath);        if (!is_int($size)) {            throw new SodiumException('Could not obtain the file size');        }         /** @var resource $fp */        $fp = fopen($filePath, 'rb');        if (!is_resource($fp)) {            throw new SodiumException('Could not open input file for reading');        }        $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength);        while ($size > 0) {            $blockSize = $size > 64                ? 64                : $size;            $read = fread($fp, $blockSize);            if (!is_string($read)) {                throw new SodiumException('Could not read input file');            }            ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read);            $size -= $blockSize;        }         fclose($fp);        return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength);    }

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.