hash_hmac

Compat function to mimic hash_hmac().

Description

(string|false) hash_hmac( (string) $algo, (string) $data, (string) $key, (constant) $raw_output = false ); 

Returns (string|false)

The hash in output determined by `$raw_output`. False if `$algo` is unknown or invalid.

Parameters (4)

0. $algo (string)
Hash algorithm. Accepts md5 or sha1..
1. $data (string)
Data to be hashed.
2. $key (string)
Secret key to use for generating the hash.
3. $raw_output — Optional. (constant) => false
Whether to output raw binary data (true), or lowercase hexits (false). Default false.

Usage

  1. if ( !function_exists( 'hash_hmac' ) ) { 
  2. require_once ABSPATH . WPINC . '/compat.php'; 
  3.  
  4. // Hash algorithm. Accepts 'md5' or 'sha1'. 
  5. $algo = ''; 
  6.  
  7. // Data to be hashed. 
  8. $data = ''; 
  9.  
  10. // Secret key to use for generating the hash. 
  11. $key = ''; 
  12.  
  13. // Optional. Whether to output raw binary data (true), 
  14. // or lowercase hexits (false). Default false. 
  15. $raw_output = false; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = hash_hmac($algo, $data, $key, $raw_output); 
  19.  

Defined (2)

The function is defined in the following location(s).

/wp-includes/compat.php  
  1. function hash_hmac($algo, $data, $key, $raw_output = false) { 
  2. return _hash_hmac($algo, $data, $key, $raw_output); 
  1. function _hash_hmac($algo, $data, $key, $raw_output = false) { 
  2. $packs = array('md5' => 'H32', 'sha1' => 'H40'); 
  3.  
  4. if ( !isset($packs[$algo]) ) 
  5. return false; 
  6.  
  7. $pack = $packs[$algo]; 
  8.  
  9. if (strlen($key) > 64) 
  10. $key = pack($pack, $algo($key)); 
  11.  
  12. $key = str_pad($key, 64, chr(0)); 
  13.  
  14. $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64)); 
  15. $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64)); 
  16.  
  17. $hmac = $algo($opad . pack($pack, $algo($ipad . $data))); 
  18.  
  19. if ( $raw_output ) 
  20. return pack( $pack, $hmac ); 
  21. return $hmac;