wp_hash( string $data, string $scheme = 'auth', string $algo = 'md5' ): string

Gets the hash of the given string.

Description

The default algorithm is md5 but can be changed to any algorithm supported by hash_hmac(). Use the hash_hmac_algos() function to check the supported algorithms.

Parameters

$datastringrequired
Plain text to hash.
$schemestringoptional
Authentication scheme (auth, secure_auth, logged_in, nonce).

Default:'auth'

$algostringoptional
Hashing algorithm to use. Default: 'md5'.

Default:'md5'

Return

string Hash of $data.

More Information

Usage:
wp_hash( $data, $scheme );
Notes:
  • This function can be replaced via plugins. If plugins do not redefine these functions, then this will be used instead.
  • Uses: wp_salt() Get WordPress salt.

Source

function wp_hash( $data, $scheme = 'auth', $algo = 'md5' ) {
	$salt = wp_salt( $scheme );

	// Ensure the algorithm is supported by the hash_hmac function.
	if ( ! in_array( $algo, hash_hmac_algos(), true ) ) {
		throw new InvalidArgumentException(
			sprintf(
				/* translators: 1: Name of a cryptographic hash algorithm. 2: List of supported algorithms. */
				__( 'Unsupported hashing algorithm: %1$s. Supported algorithms are: %2$s' ),
				$algo,
				implode( ', ', hash_hmac_algos() )
			)
		);
	}

	return hash_hmac( $algo, $data, $salt );
}

Changelog

VersionDescription
6.8.0The $algo parameter was added.
2.0.3Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.