wppaste
WordPress

wp_hash_password( string $password ): string

Since
2.5.0, 6.8.0
Source
wp-includes/pluggable.php:2648
Creates a hash of a plain text password.

Description

For integration with other applications, this function can be overwritten to instead use the other package password hashing algorithm.

Compatibility

WordPress
since 6.8.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

$passwordstring
Plain text user password to hash.

Return value

string
The hash string of the password.

Performance profile

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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
8–38

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

Plugin surface
2 hooks

Third-party callbacks on 'wp_hash_password_algorithm', 'wp_hash_password_options' run inside this call, and their cost is not bounded by anything here.

Called by
3

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

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 4 distinct outcomes

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

WhenInstructionsCalls it makes
empty($wp_hasher)8none
!empty($wp_hasher)9->HashPassword()
empty($wp_hasher) && $algorithm !== "2y"26apply_filters(), apply_filters(), password_hash()
empty($wp_hasher) && $algorithm === "2y"38apply_filters(), apply_filters(), hash_hmac(), base64_encode(), password_hash()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev508–383
8.5508–383
8.4508–3834 fewer instructions than PHP 8.3
8.3548–403
8.2548–403
8.1548–403
7.4548–403

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.

Hooks and filters fired · 2

2 hooks fire while wp_hash_password() runs, in this order:

  1. apply_filters( wp_hash_password_algorithm )filterline 2683 (+35 into the body)

    Filters the hashing algorithm to use in the password_hash() and password_needs_rehash() functions.

  2. apply_filters( wp_hash_password_options )filterline 2698 (+50 into the body)

    Filters the options passed to the password_hash() and password_needs_rehash() functions.

Uses · 1

  • apply_filters()Calls the callback functions that have been added to a filter hook.

Used by · 3

Source code

	function wp_hash_password(		#[\SensitiveParameter]		$password	) {		global $wp_hasher; 		if ( ! empty( $wp_hasher ) ) {			return $wp_hasher->HashPassword( trim( $password ) );		} 		if ( strlen( $password ) > 4096 ) {			return '*';		} 		/**		 * Filters the hashing algorithm to use in the password_hash() and password_needs_rehash() functions.		 *		 * The default is the value of the `PASSWORD_BCRYPT` constant which means bcrypt is used.		 *		 * **Important:** The only password hashing algorithm that is guaranteed to be available across PHP		 * installations is bcrypt. If you use any other algorithm you must make sure that it is available on		 * the server. The `password_algos()` function can be used to check which hashing algorithms are available.		 *		 * The hashing options can be controlled via the {@see 'wp_hash_password_options'} filter.		 *		 * Other available constants include:		 *		 * - `PASSWORD_ARGON2I`		 * - `PASSWORD_ARGON2ID`		 * - `PASSWORD_DEFAULT`		 *		 * @since 6.8.0		 *		 * @param string $algorithm The hashing algorithm. Default is the value of the `PASSWORD_BCRYPT` constant.		 */		$algorithm = apply_filters( 'wp_hash_password_algorithm', PASSWORD_BCRYPT ); 		/**		 * Filters the options passed to the password_hash() and password_needs_rehash() functions.		 *		 * The default hashing algorithm is bcrypt, but this can be changed via the {@see 'wp_hash_password_algorithm'}		 * filter. You must ensure that the options are appropriate for the algorithm in use.		 *		 * @since 6.8.0		 *		 * @param array $options    Array of options to pass to the password hashing functions.		 *                          By default this is an empty array which means the default		 *                          options will be used.		 * @param string $algorithm The hashing algorithm in use.		 */		$options = apply_filters( 'wp_hash_password_options', array(), $algorithm ); 		// Algorithms other than bcrypt don't need to use pre-hashing.		if ( PASSWORD_BCRYPT !== $algorithm ) {			return password_hash( $password, $algorithm, $options );		} 		// Use SHA-384 to retain entropy from a password that's longer than 72 bytes, and a `wp-sha384` key for domain separation.		$password_to_hash = base64_encode( hash_hmac( 'sha384', trim( $password ), 'wp-sha384', true ) ); 		// Add a prefix to facilitate distinguishing vanilla bcrypt hashes.		return '$wp' . password_hash( $password_to_hash, $algorithm, $options );	}

Changelog

Introduced in 2.5.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.

6.8.0
The password is now hashed using bcrypt by default instead of phpass.from the docblock
2.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/pluggable.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.