='); } /** * Hash a password with password_hash() or crypt() * * @param string $password * @param int $algo * * @return string * @throws AuthenticationException */ public static function hash($password, $algo = null) { if (static::supportsModernAPI() and $algo !== self::PASSWORD_ALGO_FALLBACK) { if ($algo === null) { $algo = PASSWORD_DEFAULT; } $p = password_hash($password, $algo); if ($p === false) { throw new AuthenticationException('Could not hash password, password_hash() returned false!'); } } else { $p = crypt($password, self::COMPAT_HASH . static::generateSalt()); if (strlen($p) < 13) { throw new AuthenticationException('Hash generated by crypt() seems too small, this suggests an error!'); } } return $p; } /** * Verify a password with either password_verify() or crypt() * * @param string $password * @param string $hash * * @return bool */ public static function verify($password, $hash) { if (static::supportsModernAPI()) { return password_verify($password, $hash); } else { return crypt($password, $hash) === $hash; } } /** * Shorthand to generate a salt to use with crypt() * * @return string */ public static function generateSalt() { return bin2hex(openssl_random_pseudo_bytes(self::COMPAT_SALT_LENGTH / 2)); } }