wppaste
WordPress

ParagonIE_Sodium_File::sign_core32( string $filePath, string $secretKey ): string

Source
wp-includes/sodium_compat/src/File.php:1223
Sign a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces the same result. (32-bit)

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
$secretKeystring
Secret signing key

Return value

string
Ed25519 signature

Performance profile

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

Scaling
Constant

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

Instructions
12–158

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

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

WhenInstructionsCalls it makes
!is_int($size)12filesize()
is_int($size) && !is_resource($fp)19filesize(), fopen()
is_int($size) && is_resource($fp)158filesize(), fopen(), ::substr(), hash(), ::chrToInt(), ::intToChr(), ::chrToInt(), ::intToChr(), hash_init(), ::substr(), ::hash_update(), ::updateHashWithFile(), hash_final(), ::substr(), ::ParagonIE_Sodium_Core32_Ed25519(), ::substr(), ::ParagonIE_Sodium_Core32_Ed25519(), ::ParagonIE_Sodium_Core32_Ed25519(), hash_init(), ::substr(), ::hash_update(), ::substr(), ::hash_update(), ::updateHashWithFile(), hash_final(), ::ParagonIE_Sodium_Core32_Ed25519(), ::ParagonIE_Sodium_Core32_Ed25519(), ::substr(), ::substr(), ::ParagonIE_Sodium_Compat(), 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: 168 instructions, 12–158 executed per call, 2 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 · 11

  • SodiumException::__construct()
  • ParagonIE_Sodium_File::substr()
  • ParagonIE_Sodium_File::intToChr()
  • ParagonIE_Sodium_File::chrToInt()
  • ParagonIE_Sodium_File::hash_update()
  • ParagonIE_Sodium_File::updateHashWithFile()Update a hash context with the contents of a file, without loading the entire file into memory.
  • ParagonIE_Sodium_Core32_Ed25519::sc_reduce()
  • ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes()
  • ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base()
  • ParagonIE_Sodium_Core32_Ed25519::sc_muladd()
  • ParagonIE_Sodium_Compat::memzero()It's actually not possible to zero memory buffers in PHP. You need the native library for that.

Used by · 1

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

Source code

    private static function sign_core32($filePath, $secretKey)    {        $size = filesize($filePath);        if (!is_int($size)) {            throw new SodiumException('Could not obtain the file size');        }         $fp = fopen($filePath, 'rb');        if (!is_resource($fp)) {            throw new SodiumException('Could not open input file for reading');        }         /** @var string $az */        $az = hash('sha512', self::substr($secretKey, 0, 32), true);         $az[0] = self::intToChr(self::chrToInt($az[0]) & 248);        $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64);         $hs = hash_init('sha512');        self::hash_update($hs, self::substr($az, 32, 32));        /** @var resource $hs */        $hs = self::updateHashWithFile($hs, $fp, $size);         $nonceHash = hash_final($hs, true);        $pk = self::substr($secretKey, 32, 32);        $nonce = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);        $sig = ParagonIE_Sodium_Core32_Ed25519::ge_p3_tobytes(            ParagonIE_Sodium_Core32_Ed25519::ge_scalarmult_base($nonce)        );         $hs = hash_init('sha512');        self::hash_update($hs, self::substr($sig, 0, 32));        self::hash_update($hs, self::substr($pk, 0, 32));        /** @var resource $hs */        $hs = self::updateHashWithFile($hs, $fp, $size);         $hramHash = hash_final($hs, true);         $hram = ParagonIE_Sodium_Core32_Ed25519::sc_reduce($hramHash);         $sigAfter = ParagonIE_Sodium_Core32_Ed25519::sc_muladd($hram, $az, $nonce);         /** @var string $sig */        $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);         try {            ParagonIE_Sodium_Compat::memzero($az);        } catch (SodiumException $ex) {            $az = null;        }        fclose($fp);        return $sig;    }

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.