wppaste
WordPress

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

Source
wp-includes/sodium_compat/src/File.php:613
Sign a file (rather than a string). Uses less memory than ParagonIE_Sodium_Compat::crypto_sign_detached(), 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

$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() 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
11–172

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

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

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

WhenInstructionsCalls it makes
always11–13none
is_string($filePath) && is_string($secretKey)16::strlen()
is_string($filePath) && is_string($secretKey) && !file_exists()20::strlen(), file_exists()
is_string($filePath) && is_string($secretKey) && file_exists() && !is_int($size)26::strlen(), file_exists(), filesize()
is_string($filePath) && is_string($secretKey) && file_exists() && is_int($size) && !is_resource($fp)33::strlen(), file_exists(), filesize(), fopen()
is_string($filePath) && is_string($secretKey) && file_exists() && is_int($size) && is_resource($fp)172::strlen(), file_exists(), filesize(), fopen(), ::substr(), hash(), ::chrToInt(), ::intToChr(), ::chrToInt(), ::intToChr(), hash_init(), ::substr(), ::hash_update(), ::updateHashWithFile(), hash_final(), ::substr(), ::ParagonIE_Sodium_Core_Ed25519(), ::substr(), ::ParagonIE_Sodium_Core_Ed25519(), ::ParagonIE_Sodium_Core_Ed25519(), hash_init(), ::substr(), ::hash_update(), ::substr(), ::hash_update(), ::updateHashWithFile(), hash_final(), ::ParagonIE_Sodium_Core_Ed25519(), ::ParagonIE_Sodium_Core_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: 204 instructions, 11–172 executed per call, 6 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 · 14

  • TypeError::__construct()
  • ParagonIE_Sodium_File::strlen()
  • 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)
  • 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_Core_Ed25519::sc_reduce()
  • ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes()
  • ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base()
Show all 14
  • ParagonIE_Sodium_Core_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.

Source code

    public static function sign(        $filePath,        #[\SensitiveParameter]        $secretKey    ) {        /* Type checks: */        if (!is_string($filePath)) {            throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.');        }        if (!is_string($secretKey)) {            throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.');        }         /* Input validation: */        if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) {            throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes');        }        if (PHP_INT_SIZE === 4) {            return self::sign_core32($filePath, $secretKey);        }         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');        }         /** @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);         /** @var string $nonceHash */        $nonceHash = hash_final($hs, true);         /** @var string $pk */        $pk = self::substr($secretKey, 32, 32);         /** @var string $nonce */        $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32);         /** @var string $sig */        $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes(            ParagonIE_Sodium_Core_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);         /** @var string $hramHash */        $hramHash = hash_final($hs, true);         /** @var string $hram */        $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash);         /** @var string $sigAfter */        $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce);         /** @var string $sig */        $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32);         try {

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.