ParagonIE_Sodium_File::generichash( string $filePath, string|null $key = '', int $outputLength = 32 ): string
- Source
wp-includes/sodium_compat/src/File.php:380
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
- Scaling
- Scales with input
- Instructions
- 12–77
- Plugin surface
- None
- Called by
- 0
Reads or writes the filesystem via file_exists().
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 140.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- filesystemfilesystem access
file_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12–29 | none |
is_string($filePath) && !empty($key) | 21–29 | ::strlen() |
is_string($filePath) && empty($key) && !$outputLength && !file_exists() | 25–33 | file_exists() |
is_string($filePath) && !empty($key) | 27–41 | ::strlen(), ::strlen() |
is_string($filePath) && empty($key) && !$outputLength && file_exists() && !is_int($size) | 30–38 | file_exists(), filesize() |
is_string($filePath) && empty($key) && !$outputLength && file_exists() && is_int($size) && !is_resource($fp) | 37–45 | file_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) && !$size | 49–57 | file_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–65 | file_exists(), filesize(), fopen(), ::ParagonIE_Sodium_Compat(), fread() |
is_string($filePath) && !empty($key) && !$outputLength && file_exists() && is_int($size) && is_resource($fp) && !$size | 61–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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 140 | 12–77 | 16 | |
| 8.5 | 140 | 12–77 | 16 | |
| 8.4 | 140 | 12–77 | 16 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 142 | 12–79 | 16 | |
| 8.2 | 142 | 12–79 | 16 | |
| 8.1 | 142 | 12–79 | 16 | |
| 7.4 | 142 | 12–79 | 16 |
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
- TypeError::__construct()
- ParagonIE_Sodium_File::strlen()
- SodiumException::__construct()
- ParagonIE_Sodium_Compat::crypto_generichash_init()Initialize a BLAKE2b hashing context, for use in a streaming interface.
- ParagonIE_Sodium_Compat::crypto_generichash_update()Update a BLAKE2b hashing context with additional data.
- ParagonIE_Sodium_Compat::crypto_generichash_final()Get the final BLAKE2b hash output for a given context.
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.
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.