Emarref\Jwt\Encryption\Symmetric::timingSafeEquals PHP Метод

timingSafeEquals() публичный Метод

A timing safe equals comparison.
См. также: http://blog.ircmaxell.com/2014/11/its-all-about-time.html
public timingSafeEquals ( string $safe, string $user ) : boolean
$safe string The internal (safe) value to be checked
$user string The user submitted (unsafe) value
Результат boolean True if the two strings are identical.
    public function timingSafeEquals($safe, $user)
    {
        if (function_exists('hash_equals')) {
            return hash_equals($user, $safe);
        }
        $safeLen = strlen($safe);
        $userLen = strlen($user);
        /*
         * In general, it's not possible to prevent length leaks. So it's OK to leak the length.
         * @see http://security.stackexchange.com/questions/49849/timing-safe-string-comparison-avoiding-length-leak
         */
        if ($userLen != $safeLen) {
            return false;
        }
        $result = 0;
        for ($i = 0; $i < $userLen; $i++) {
            $result |= ord($safe[$i]) ^ ord($user[$i]);
        }
        return $result === 0;
    }