wppaste
WordPress

verify_file_signature( string $filename, string|array $signatures, string|false $filename_for_errors = false ): bool|WP_Error

Since
5.2.0
Source
wp-admin/includes/file.php:1399
Verifies the contents of a file against its ED25519 signature.

Compatibility

WordPress
since 5.2.0
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

$filenamestring
The file to validate.
$signaturesstring|array
A Signature provided for the file.
$filename_for_errorsstring|falseoptional
A friendly filename for errors.Default: false

Return value

bool|WP_Error
True on success, false if verification not attempted, or WP_Error describing an error condition.

Performance profile

How much work a call to verify_file_signature() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
29–64

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

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

  • hookthird-party callbacksapply_filters()one call below verify_file_signature()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

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

WhenInstructionsCalls it makes
always29array_map(), __(), esc_html(), sprintf()
always31array_map(), __(), esc_html(), sprintf(), filename()
always33wp_basename(), array_map(), __(), esc_html(), sprintf()
always35wp_basename(), array_map(), __(), esc_html(), sprintf(), filename()
!$signatures && !$trusted_keys && sodium_crypto_sign_verify_detached()56array_map(), wp_trusted_keys(), hash_file(), mbstring_binary_safe_encoding(), base64_decode(), base64_decode(), sodium_crypto_sign_verify_detached(), reset_mbstring_encoding()
always59–60array_map(), wp_trusted_keys(), hash_file(), mbstring_binary_safe_encoding(), reset_mbstring_encoding(), __(), esc_html(), sprintf(), bin2hex(), filename()
!$signatures && !$trusted_keys && sodium_crypto_sign_verify_detached()60wp_basename(), array_map(), wp_trusted_keys(), hash_file(), mbstring_binary_safe_encoding(), base64_decode(), base64_decode(), sodium_crypto_sign_verify_detached(), reset_mbstring_encoding()
always63–64wp_basename(), array_map(), wp_trusted_keys(), hash_file(), mbstring_binary_safe_encoding(), reset_mbstring_encoding(), __(), esc_html(), sprintf(), bin2hex(), filename()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev13529–6410
8.513529–6410
8.413529–64103 fewer instructions than PHP 8.3
8.313832–67102 fewer instructions than PHP 8.2
8.214034–6910
8.114034–6910
7.414034–6910

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

Used by · 1

  • download_url()Downloads a URL to a local temporary file using the WordPress HTTP API.

Source code

function verify_file_signature( $filename, $signatures, $filename_for_errors = false ) {	if ( ! $filename_for_errors ) {		$filename_for_errors = wp_basename( $filename );	} 	// Check we can process signatures.	if ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) || ! in_array( 'sha384', array_map( 'strtolower', hash_algos() ), true ) ) {		return new WP_Error(			'signature_verification_unsupported',			sprintf(				/* translators: %s: The filename of the package. */				__( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),				'<span class="code">' . esc_html( $filename_for_errors ) . '</span>'			),			( ! function_exists( 'sodium_crypto_sign_verify_detached' ) ? 'sodium_crypto_sign_verify_detached' : 'sha384' )		);	} 	// Verify runtime speed of Sodium_Compat is acceptable.	if ( ! extension_loaded( 'sodium' ) && ! ParagonIE_Sodium_Compat::polyfill_is_fast() ) {		$sodium_compat_is_fast = false; 		// Allow for an old version of Sodium_Compat being loaded before the bundled WordPress one.		if ( method_exists( 'ParagonIE_Sodium_Compat', 'runtime_speed_test' ) ) {			/*			 * Run `ParagonIE_Sodium_Compat::runtime_speed_test()` in optimized integer mode,			 * as that's what WordPress utilizes during signing verifications.			 */			// phpcs:disable WordPress.NamingConventions.ValidVariableName			$old_fastMult                      = ParagonIE_Sodium_Compat::$fastMult;			ParagonIE_Sodium_Compat::$fastMult = true;			$sodium_compat_is_fast             = ParagonIE_Sodium_Compat::runtime_speed_test( 100, 10 );			ParagonIE_Sodium_Compat::$fastMult = $old_fastMult;			// phpcs:enable		} 		/*		 * This cannot be performed in a reasonable amount of time.		 * https://github.com/paragonie/sodium_compat#help-sodium_compat-is-slow-how-can-i-make-it-fast		 */		if ( ! $sodium_compat_is_fast ) {			return new WP_Error(				'signature_verification_unsupported',				sprintf(					/* translators: %s: The filename of the package. */					__( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),					'<span class="code">' . esc_html( $filename_for_errors ) . '</span>'				),				array(					'php'                => PHP_VERSION,					'sodium'             => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),					'polyfill_is_fast'   => false,					'max_execution_time' => ini_get( 'max_execution_time' ),				)			);		}	} 	if ( ! $signatures ) {		return new WP_Error(			'signature_verification_no_signature',			sprintf(				/* translators: %s: The filename of the package. */				__( 'The authenticity of %s could not be verified as no signature was found.' ),				'<span class="code">' . esc_html( $filename_for_errors ) . '</span>'			),			array(				'filename' => $filename_for_errors,			)		);	} 	$trusted_keys = wp_trusted_keys();	$file_hash    = hash_file( 'sha384', $filename, true ); 	mbstring_binary_safe_encoding(); 	$skipped_key       = 0;	$skipped_signature = 0;

Changelog

Introduced in 5.2.0. 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.0.4 tag, from src/wp-admin/includes/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.