Remplacer la fonction php 5 hash_hmac

Contexte

Sur certains serveur, on n'a pas la liberté d'installer php 5 qui supporte la fonction hash_hmac. Voici comment recréer cette fonction à partir de la bibliothèque pear.

Réalisation

  • Installer Crypt_HMAC via pear
  • pear install pear install http://download.pear.php.net/package/Crypt_HMAC-1.0.1.tgz
    
  • Déclarer la fonction hash_hmac() dans votre code :
  • <?php
    ...
    if (!function_exists('hash_hmac')) {
        require 'Crypt/HMAC.php';
        function hash_hmac($type, $data, $key)
        {
            $hmac = new Crypt_HMAC($key, $type);
            return $hmac->hash($data);
        }
    }
    ?>