public function validate($totp, $key, $stamp = null)
    {
        if ($stamp === null) {
            $stamp = $this->timeManager->getCurrentStamp();
        }

        if (!preg_match('/^[0-9]{6}$/', $totp)) {
            return false;
        }
        $totp = intval($totp);

        // Search the stamp corresponding to the totp provided
        for ($st = $stamp - $this->window; $st <= $stamp + $this->window; ++$st) {
            if (($res = TOTPGenerator::generate($st, $key)) == $totp) {
                return $st;
            }
        }

        return false;
    }