1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

feat(toxid): Added correct checksum validation.

Fix #3837
This commit is contained in:
Diadlo 2016-11-09 01:57:51 +03:00
parent c27d809e08
commit b4c4569299
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
2 changed files with 35 additions and 4 deletions

View File

@ -76,7 +76,7 @@ ToxId::ToxId(const QString &id)
{ {
publicKey = id.left(TOX_ID_PUBLIC_KEY_LENGTH); publicKey = id.left(TOX_ID_PUBLIC_KEY_LENGTH);
noSpam = id.mid(TOX_ID_PUBLIC_KEY_LENGTH, TOX_ID_NO_SPAM_LENGTH); noSpam = id.mid(TOX_ID_PUBLIC_KEY_LENGTH, TOX_ID_NO_SPAM_LENGTH);
checkSum = id.mid(TOX_ID_PUBLIC_KEY_LENGTH + TOX_ID_NO_SPAM_LENGTH, TOX_ID_CHECKSUM_LENGTH); checkSum = id.right(TOX_ID_CHECKSUM_LENGTH);
} }
else else
{ {
@ -124,12 +124,42 @@ void ToxId::clear()
} }
/** /**
* @brief Check, that id is a valid Tox ID. * @brief Check, that id is probably a valid Tox ID.
* @param id Tox ID to check. * @param id Tox ID to check.
* @return True if id is a valid Tox ID, false otherwise. * @return True if the string can be a ToxID, false otherwise.
* @note Doesn't validate checksum.
*/ */
bool ToxId::isToxId(const QString &id) bool ToxId::isValidToxId(const QString& id)
{ {
const QRegularExpression hexRegExp("^[A-Fa-f0-9]+$"); const QRegularExpression hexRegExp("^[A-Fa-f0-9]+$");
return id.length() == TOX_HEX_ID_LENGTH && id.contains(hexRegExp); return id.length() == TOX_HEX_ID_LENGTH && id.contains(hexRegExp);
} }
/**
* @brief Check, that id is a valid Tox ID.
* @param id Tox ID to check.
* @return True if id is a valid Tox ID, false otherwise.
*/
bool ToxId::isToxId(const QString& id)
{
if (!isValidToxId(id))
{
return false;
}
uint32_t size = TOX_ID_PUBLIC_KEY_LENGTH + TOX_ID_NO_SPAM_LENGTH;
QString publicKeyStr = id.left(size);
QString checksumStr = id.right(TOX_ID_CHECKSUM_LENGTH);
QByteArray publicKey = QByteArray::fromHex(publicKeyStr.toLatin1());
QByteArray checksum = QByteArray::fromHex(checksumStr.toLatin1());
uint8_t check[2] = {0};
for (uint32_t i = 0; i < size / 2; i++)
{
check[i % 2] ^= publicKey.data()[i];
}
QByteArray caclulated(reinterpret_cast<char*>(check), 2);
return caclulated == checksum;
}

View File

@ -36,6 +36,7 @@ public:
void clear(); void clear();
static bool isToxId(const QString& id); static bool isToxId(const QString& id);
static bool isValidToxId(const QString &id);
public: public:
QString publicKey; QString publicKey;