_hash_hmac()WP 3.2.0

Internal compat function to mimic hash_hmac().

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

No Hooks.

Return

String|false. The hash in output determined by $binary. False if $algo is unknown or invalid.

Usage

_hash_hmac( $algo, $data, $key, $binary );
$algo(string) (required)
Hash algorithm. Accepts 'md5' or 'sha1'.
$data(string) (required)
Data to be hashed.
$key(string) (required)
Secret key to use for generating the hash.
$binary(true|false)
Whether to output raw binary data (true), or lowercase hexits (false).
Default: false

Changelog

Since 3.2.0 Introduced.

_hash_hmac() code WP 6.7.1

function _hash_hmac( $algo, $data, $key, $binary = false ) {
	$packs = array(
		'md5'  => 'H32',
		'sha1' => 'H40',
	);

	if ( ! isset( $packs[ $algo ] ) ) {
		return false;
	}

	$pack = $packs[ $algo ];

	if ( strlen( $key ) > 64 ) {
		$key = pack( $pack, $algo( $key ) );
	}

	$key = str_pad( $key, 64, chr( 0 ) );

	$ipad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x36 ), 64 ) );
	$opad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x5C ), 64 ) );

	$hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) );

	if ( $binary ) {
		return pack( $pack, $hmac );
	}

	return $hmac;
}