Esempio n. 1
0
static QByteArray deriveKeyPbkdf2(QCryptographicHash::Algorithm algorithm,
                                  const QByteArray &data, const QByteArray &salt,
                                  int iterations, quint64 dkLen)
{
    QByteArray key;
    quint32 currentIteration = 1;
    QMessageAuthenticationCode hmac(algorithm, data);
    QByteArray index(4, Qt::Uninitialized);
    while (quint64(key.length()) < dkLen) {
        hmac.addData(salt);
        qToBigEndian(currentIteration, reinterpret_cast<uchar*>(index.data()));
        hmac.addData(index);
        QByteArray u = hmac.result();
        hmac.reset();
        QByteArray tkey = u;
        for (int iter = 1; iter < iterations; iter++) {
            hmac.addData(u);
            u = hmac.result();
            hmac.reset();
            std::transform(tkey.cbegin(), tkey.cend(), u.cbegin(), tkey.begin(),
                           std::bit_xor<char>());
        }
        key += tkey;
        currentIteration++;
    }
    return key.left(dkLen);
}
Esempio n. 2
0
void EditDialog::generate_password(int count, bool capital, bool small, bool digits)
{
    qsrand(QDateTime::currentMSecsSinceEpoch());
    int remain = count;
    QString password;
    while(remain > 0) {
        QByteArray sampleArray;
        for(int i = 0; i < 256; i++) {
            sampleArray.append((char)(qrand() % 256));
        }
        QByteArray destArray = QCryptographicHash::hash(sampleArray, QCryptographicHash::Md5).toBase64();
        for(auto iter = destArray.cbegin(); iter != destArray.cend(); iter++) {
            char c = *iter;
            if((capital && c >= 'A'  && c <= 'Z') ||
                    (small && c >= 'a' && c <= 'z') ||
                    (digits && c >= '0' && c <= '9')) {
                password.append(c);
                remain--;
                if(remain == 0)  break;
            }
        }
    }
    ui->passwordEdit->setText(password);
    ui->passwordAgainEdit->setText(password);
}
Esempio n. 3
0
bool QXmppSaslClientScram::respond(const QByteArray &challenge, QByteArray &response)
{
    Q_UNUSED(challenge);
    if (m_step == 0) {
        m_gs2Header = "n,,";
        m_clientFirstMessageBare = "n=" + username().toUtf8() + ",r=" + m_nonce;

        response = m_gs2Header + m_clientFirstMessageBare;
        m_step++;
        return true;
    } else if (m_step == 1) {
        // validate input
        const QMap<char, QByteArray> input = parseGS2(challenge);
        const QByteArray nonce = input.value('r');
        const QByteArray salt = QByteArray::fromBase64(input.value('s'));
        const int iterations = input.value('i').toInt();
        if (!nonce.startsWith(m_nonce) || salt.isEmpty() || iterations < 1) {
            return false;
        }

        // calculate proofs
        const QByteArray clientFinalMessageBare = "c=" + m_gs2Header.toBase64() + ",r=" + nonce;
        const QByteArray saltedPassword = deriveKeyPbkdf2(m_algorithm, password().toUtf8(), salt,
                                                          iterations, m_dklen);
        const QByteArray clientKey = QMessageAuthenticationCode::hash("Client Key", saltedPassword, m_algorithm);
        const QByteArray storedKey = QCryptographicHash::hash(clientKey, m_algorithm);
        const QByteArray authMessage = m_clientFirstMessageBare + "," + challenge + "," + clientFinalMessageBare;
        QByteArray clientProof = QMessageAuthenticationCode::hash(authMessage, storedKey, m_algorithm);
        std::transform(clientProof.cbegin(), clientProof.cend(), clientKey.cbegin(),
                       clientProof.begin(), std::bit_xor<char>());

        const QByteArray serverKey = QMessageAuthenticationCode::hash("Server Key", saltedPassword, m_algorithm);
        m_serverSignature = QMessageAuthenticationCode::hash(authMessage, serverKey, m_algorithm);

        response = clientFinalMessageBare + ",p=" + clientProof.toBase64();
        m_step++;
        return true;
    } else if (m_step == 2) {
        const QMap<char, QByteArray> input = parseGS2(challenge);
        response = QByteArray();
        m_step++;
        return QByteArray::fromBase64(input.value('v')) == m_serverSignature;
    } else {
        warning("QXmppSaslClientPlain : Invalid step");
        return false;
    }
}
Esempio n. 4
0
std::function<bool(u32)> CheatsManager::CreateMatchFunction()
{
  const QString text = m_match_value->text();

  if (text.isEmpty())
  {
    m_result_label->setText(tr("No search value entered."));
    return nullptr;
  }

  const CompareType op = static_cast<CompareType>(m_match_operation->currentIndex());

  const int base =
      (m_match_decimal->isChecked() ? 10 : (m_match_hexadecimal->isChecked() ? 16 : 8));

  bool conversion_succeeded = false;
  std::function<bool(u32)> matches_func;
  switch (static_cast<DataType>(m_match_length->currentIndex()))
  {
  case DataType::Byte:
  {
    u8 comparison_value = text.toUShort(&conversion_succeeded, base) & 0xFF;
    matches_func = [=](u32 addr) {
      return Compare<u8>(PowerPC::HostRead_U8(addr), comparison_value, op);
    };
    break;
  }
  case DataType::Short:
  {
    u16 comparison_value = text.toUShort(&conversion_succeeded, base);
    matches_func = [=](u32 addr) {
      return Compare(PowerPC::HostRead_U16(addr), comparison_value, op);
    };
    break;
  }
  case DataType::Int:
  {
    u32 comparison_value = text.toUInt(&conversion_succeeded, base);
    matches_func = [=](u32 addr) {
      return Compare(PowerPC::HostRead_U32(addr), comparison_value, op);
    };
    break;
  }
  case DataType::Float:
  {
    float comparison_value = text.toFloat(&conversion_succeeded);
    matches_func = [=](u32 addr) {
      return Compare(PowerPC::HostRead_F32(addr), comparison_value, op);
    };
    break;
  }
  case DataType::Double:
  {
    double comparison_value = text.toDouble(&conversion_succeeded);
    matches_func = [=](u32 addr) {
      return Compare(PowerPC::HostRead_F64(addr), comparison_value, op);
    };
    break;
  }
  case DataType::String:
  {
    if (op != CompareType::Equal && op != CompareType::NotEqual)
    {
      m_result_label->setText(tr("String values can only be compared using equality."));
      return nullptr;
    }

    conversion_succeeded = true;

    const QString lambda_text = m_match_value->text();
    const QByteArray utf8_bytes = lambda_text.toUtf8();

    matches_func = [op, utf8_bytes](u32 addr) {
      bool is_equal = std::equal(utf8_bytes.cbegin(), utf8_bytes.cend(),
                                 reinterpret_cast<char*>(Memory::m_pRAM + addr - 0x80000000));
      switch (op)
      {
      case CompareType::Equal:
        return is_equal;
      case CompareType::NotEqual:
        return !is_equal;
      default:
        // This should never occur since we've already checked the type of op
        return false;
      }
    };
    break;
  }
  }

  if (conversion_succeeded)
    return matches_func;

  m_result_label->setText(tr("Cannot interpret the given value.\nHave you chosen the right type?"));
  return nullptr;
}
Esempio n. 5
0
std::string KulloClient2Qt::fromUtf8(const QByteArray &in)
{
    return std::string(in.cbegin(), in.cend());
}