Source for file hash_equals.php

Documentation is available at hash_equals.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage auth
  5. * @author     Laurent Jouanneau
  6. * @copyright  2015 Laurent Jouanneau
  7. * @licence  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  8. */
  9.  
  10. if(!function_exists('hash_equals')) {
  11.     // already defined in PHP 5.6
  12.     function hash_equals($known_str, $user_str) {
  13.         if (!is_string($known_str)) {
  14.             trigger_error("Expected known_str to be a string, $known_str given", E_USER_WARNING);
  15.             return false;
  16.         }
  17.         if (!is_string($user_str)) {
  18.             trigger_error("Expected user_str to be a string, $user_str given", E_USER_WARNING);
  19.             return false;
  20.         }
  21.  
  22.         $known_len = strlen($known_str);
  23.         $user_len = strlen($user_str);
  24.         if ($known_len != $user_len) {
  25.             // if different lengths, do comparison as well, to have time constant
  26.             // but return false as expected
  27.             $user_str = $known_str;
  28.             $result = 1;
  29.         }
  30.         else {
  31.             $result = 0;
  32.         }
  33.  
  34.         /* This is security sensitive code. Do not optimize this for speed. */
  35.         for ($j = 0; $j < $known_len; $j++) {
  36.            $result |= ord($known_str[$j] ^ $user_str[$j]);
  37.         }
  38.         return ($result == 0);
  39.     }
  40. }

Documentation generated on Wed, 04 Jan 2017 22:51:44 +0100 by phpDocumentor 1.4.3