TheQuack45

password_verify() function

Jul 15th, 2015
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. /**
  2.          * Verify a password against a hash using a timing attack resistant approach
  3.          *
  4.          * @param string $password The password to verify
  5.          * @param string $hash     The hash to verify against
  6.          *
  7.          * @return boolean If the password matches the hash
  8.          */
  9.         function password_verify($password, $hash) {
  10.             if (!function_exists('crypt')) {
  11.                 trigger_error("Crypt must be loaded for password_verify to function", E_USER_WARNING);
  12.                 return false;
  13.             }
  14.             $ret = crypt($password, $hash);
  15.             if (!is_string($ret) || PasswordCompat\binary\_strlen($ret) != PasswordCompat\binary\_strlen($hash) || PasswordCompat\binary\_strlen($ret) <= 13) {
  16.                 return false;
  17.             }
  18.             $status = 0;
  19.             for ($i = 0; $i < PasswordCompat\binary\_strlen($ret); $i++) {
  20.                 $status |= (ord($ret[$i]) ^ ord($hash[$i]));
  21.             }
  22.             return $status === 0;
  23.         }
Advertisement
Add Comment
Please, Sign In to add comment