2015-06-06 09:40:08 +08:00
|
|
|
/*
|
2018-04-13 04:02:28 +08:00
|
|
|
Copyright © 2015-2018 by The qTox Project Contributors
|
2015-06-06 09:40:08 +08:00
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-05-18 02:17:14 +08:00
|
|
|
#include "toxid.h"
|
2016-12-30 06:14:48 +08:00
|
|
|
#include "toxpk.h"
|
2015-05-18 04:48:24 +08:00
|
|
|
|
2015-05-18 02:17:14 +08:00
|
|
|
#include <tox/tox.h>
|
2016-12-26 08:39:22 +08:00
|
|
|
|
2016-12-28 22:43:11 +08:00
|
|
|
#include <QRegularExpression>
|
2017-02-26 19:52:45 +08:00
|
|
|
#include <cstdint>
|
2015-05-18 02:17:14 +08:00
|
|
|
|
2016-12-26 04:37:37 +08:00
|
|
|
// Tox doesn't publicly define these
|
2017-02-26 19:52:45 +08:00
|
|
|
#define NOSPAM_BYTES 4
|
|
|
|
#define CHECKSUM_BYTES 2
|
2016-12-26 04:37:37 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
#define PUBLIC_KEY_HEX_CHARS (2 * TOX_PUBLIC_KEY_SIZE)
|
|
|
|
#define NOSPAM_HEX_CHARS (2 * NOSPAM_BYTES)
|
|
|
|
#define CHECKSUM_HEX_CHARS (2 * CHECKSUM_BYTES)
|
|
|
|
#define TOXID_HEX_CHARS (2 * TOX_ADDRESS_SIZE)
|
2015-05-18 02:17:14 +08:00
|
|
|
|
2016-12-30 20:49:25 +08:00
|
|
|
const QRegularExpression ToxId::ToxIdRegEx(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(TOXID_HEX_CHARS));
|
2016-12-28 22:43:11 +08:00
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
/**
|
2016-08-01 16:20:56 +08:00
|
|
|
* @class ToxId
|
|
|
|
* @brief This class represents a Tox ID.
|
|
|
|
*
|
|
|
|
* An ID is composed of 32 bytes long public key, 4 bytes long NoSpam
|
|
|
|
* and 2 bytes long checksum.
|
|
|
|
*
|
|
|
|
* e.g.
|
|
|
|
* @code
|
|
|
|
* | C7719C6808C14B77348004956D1D98046CE09A34370E7608150EAD74C3815D30 | C8BA3AB9 | BEB9
|
|
|
|
* | / |
|
|
|
|
* | / NoSpam | Checksum
|
|
|
|
* | Public Key (PK), 32 bytes, 64 characters / 4 bytes | 2 bytes
|
|
|
|
* | | 8 characters| 4 characters
|
|
|
|
* @endcode
|
|
|
|
*/
|
2016-07-27 06:21:22 +08:00
|
|
|
|
|
|
|
/**
|
2016-08-01 16:20:56 +08:00
|
|
|
* @brief The default constructor. Creates an empty Tox ID.
|
|
|
|
*/
|
2015-05-18 02:17:14 +08:00
|
|
|
ToxId::ToxId()
|
2017-02-26 19:52:45 +08:00
|
|
|
: toxId()
|
|
|
|
{
|
|
|
|
}
|
2015-05-18 02:17:14 +08:00
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
/**
|
2016-08-01 16:20:56 +08:00
|
|
|
* @brief The copy constructor.
|
|
|
|
* @param other ToxId to copy
|
|
|
|
*/
|
2016-12-26 06:16:00 +08:00
|
|
|
ToxId::ToxId(const ToxId& other)
|
2017-02-26 19:52:45 +08:00
|
|
|
: toxId(other.toxId)
|
|
|
|
{
|
|
|
|
}
|
2015-05-18 02:17:14 +08:00
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
/**
|
2016-12-26 04:37:37 +08:00
|
|
|
* @brief Create a Tox ID from a QString.
|
2016-08-01 16:20:56 +08:00
|
|
|
*
|
2016-12-26 08:39:22 +08:00
|
|
|
* If the given rawId is not a valid Tox ID, but can be a Public Key then:
|
|
|
|
* publicKey == rawId and noSpam == 0 == checkSum.
|
|
|
|
* If the given rawId isn't a valid Public Key or Tox ID a ToxId with all zero bytes is created.
|
2016-08-01 16:20:56 +08:00
|
|
|
*
|
|
|
|
* @param id Tox ID string to convert to ToxId object
|
2016-07-27 06:21:22 +08:00
|
|
|
*/
|
2016-12-26 06:16:00 +08:00
|
|
|
ToxId::ToxId(const QString& id)
|
2015-05-18 02:17:14 +08:00
|
|
|
{
|
2016-12-30 20:49:25 +08:00
|
|
|
// TODO: remove construction from PK only
|
2017-02-26 19:52:45 +08:00
|
|
|
if (isToxId(id)) {
|
2016-12-26 04:37:37 +08:00
|
|
|
toxId = QByteArray::fromHex(id.toLatin1());
|
2017-02-26 19:52:45 +08:00
|
|
|
} else if (id.length() >= PUBLIC_KEY_HEX_CHARS) {
|
2016-12-26 04:37:37 +08:00
|
|
|
toxId = QByteArray::fromHex(id.left(PUBLIC_KEY_HEX_CHARS).toLatin1());
|
2017-02-26 19:52:45 +08:00
|
|
|
} else {
|
|
|
|
toxId = QByteArray(); // invalid id string
|
2016-12-26 04:37:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Create a Tox ID from a QByteArray.
|
|
|
|
*
|
2016-12-26 08:39:22 +08:00
|
|
|
* If the given rawId is not a valid Tox ID, but can be a Public Key then:
|
|
|
|
* publicKey == rawId and noSpam == 0 == checkSum.
|
|
|
|
* If the given rawId isn't a valid Public Key or Tox ID a ToxId with all zero bytes is created.
|
2016-12-26 04:37:37 +08:00
|
|
|
*
|
2016-12-26 08:39:22 +08:00
|
|
|
* @param rawId Tox ID bytes to convert to ToxId object
|
2016-12-26 04:37:37 +08:00
|
|
|
*/
|
2016-12-26 06:16:00 +08:00
|
|
|
ToxId::ToxId(const QByteArray& rawId)
|
|
|
|
{
|
2016-12-28 22:43:11 +08:00
|
|
|
constructToxId(rawId);
|
2016-12-26 06:16:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-26 08:39:22 +08:00
|
|
|
* @brief Create a Tox ID from uint8_t bytes and lenght, convenience function for toxcore interface.
|
2016-12-26 06:16:00 +08:00
|
|
|
*
|
2016-12-26 08:39:22 +08:00
|
|
|
* If the given rawId is not a valid Tox ID, but can be a Public Key then:
|
|
|
|
* publicKey == rawId and noSpam == 0 == checkSum.
|
|
|
|
* If the given rawId isn't a valid Public Key or Tox ID a ToxId with all zero bytes is created.
|
2016-12-26 06:16:00 +08:00
|
|
|
*
|
2016-12-26 08:39:22 +08:00
|
|
|
* @param rawId Pointer to bytes to convert to ToxId object
|
|
|
|
* @param len Number of bytes to read. Must be TOX_SECRET_KEY_SIZE for a Public Key or
|
|
|
|
* TOX_ADDRESS_SIZE for a Tox ID.
|
2016-12-26 06:16:00 +08:00
|
|
|
*/
|
2016-12-28 22:43:11 +08:00
|
|
|
ToxId::ToxId(const uint8_t* rawId, int len)
|
2016-12-26 06:16:00 +08:00
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
QByteArray tmpId(reinterpret_cast<const char*>(rawId), len);
|
2016-12-28 22:43:11 +08:00
|
|
|
constructToxId(tmpId);
|
2016-12-26 06:16:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-28 22:43:11 +08:00
|
|
|
void ToxId::constructToxId(const QByteArray& rawId)
|
2016-12-26 04:37:37 +08:00
|
|
|
{
|
2016-12-30 20:49:25 +08:00
|
|
|
// TODO: remove construction from PK only
|
2017-02-26 19:52:45 +08:00
|
|
|
if (rawId.length() == TOX_SECRET_KEY_SIZE) {
|
|
|
|
toxId = QByteArray(rawId); // construct from PK only
|
|
|
|
} else if (rawId.length() == TOX_ADDRESS_SIZE && isToxId(rawId.toHex().toUpper())) {
|
|
|
|
toxId = QByteArray(rawId); // construct from full toxid
|
|
|
|
} else {
|
|
|
|
toxId = QByteArray(); // invalid id
|
2015-05-18 02:17:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
/**
|
2016-12-26 08:39:22 +08:00
|
|
|
* @brief Compares the equality of the Public Key.
|
2016-08-01 16:20:56 +08:00
|
|
|
* @param other Tox ID to compare.
|
2016-12-26 08:39:22 +08:00
|
|
|
* @return True if both Tox IDs have the same public keys, false otherwise.
|
2016-08-01 16:20:56 +08:00
|
|
|
*/
|
2015-05-18 02:17:14 +08:00
|
|
|
bool ToxId::operator==(const ToxId& other) const
|
|
|
|
{
|
2016-12-26 04:37:37 +08:00
|
|
|
return getPublicKey() == other.getPublicKey();
|
2015-05-18 02:17:14 +08:00
|
|
|
}
|
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
/**
|
2016-12-26 08:39:22 +08:00
|
|
|
* @brief Compares the inequality of the Public Key.
|
2016-08-01 16:20:56 +08:00
|
|
|
* @param other Tox ID to compare.
|
2016-12-26 08:39:22 +08:00
|
|
|
* @return True if both Tox IDs have different public keys, false otherwise.
|
2016-08-01 16:20:56 +08:00
|
|
|
*/
|
2016-12-26 06:16:00 +08:00
|
|
|
bool ToxId::operator!=(const ToxId& other) const
|
2015-05-18 02:17:14 +08:00
|
|
|
{
|
2016-12-26 04:37:37 +08:00
|
|
|
return getPublicKey() != other.getPublicKey();
|
2015-05-18 02:17:14 +08:00
|
|
|
}
|
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
/**
|
2016-12-26 08:39:22 +08:00
|
|
|
* @brief Returns the Tox ID converted to QString.
|
|
|
|
* Is equal to getPublicKey() if the Tox ID was constructed from only a Public Key.
|
2016-08-01 16:20:56 +08:00
|
|
|
* @return The Tox ID as QString.
|
|
|
|
*/
|
2015-05-18 02:17:14 +08:00
|
|
|
QString ToxId::toString() const
|
|
|
|
{
|
2016-12-26 04:37:37 +08:00
|
|
|
return toxId.toHex().toUpper();
|
2015-05-18 02:17:14 +08:00
|
|
|
}
|
|
|
|
|
2016-07-27 06:21:22 +08:00
|
|
|
/**
|
2016-08-01 16:20:56 +08:00
|
|
|
* @brief Clears all elements of the Tox ID.
|
|
|
|
*/
|
2015-05-18 02:17:14 +08:00
|
|
|
void ToxId::clear()
|
|
|
|
{
|
2016-12-26 04:37:37 +08:00
|
|
|
toxId.clear();
|
2015-05-18 02:17:14 +08:00
|
|
|
}
|
|
|
|
|
2016-12-26 06:16:00 +08:00
|
|
|
/**
|
|
|
|
* @brief Gets the ToxID as bytes, convenience function for toxcore interface.
|
2016-12-30 20:49:25 +08:00
|
|
|
* @return The ToxID as uint8_t* if isValid() is true, else a nullptr.
|
2016-12-26 06:16:00 +08:00
|
|
|
*/
|
|
|
|
const uint8_t* ToxId::getBytes() const
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
if (isValid()) {
|
2016-12-30 20:49:25 +08:00
|
|
|
return reinterpret_cast<const uint8_t*>(toxId.constData());
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
2016-12-26 06:16:00 +08:00
|
|
|
}
|
|
|
|
|
2016-12-26 04:37:37 +08:00
|
|
|
/**
|
|
|
|
* @brief Gets the Public Key part of the ToxID
|
|
|
|
* @return Public Key of the ToxID
|
|
|
|
*/
|
2016-12-30 06:14:48 +08:00
|
|
|
ToxPk ToxId::getPublicKey() const
|
2016-12-26 04:37:37 +08:00
|
|
|
{
|
2017-09-10 03:25:39 +08:00
|
|
|
return ToxPk(toxId.left(TOX_PUBLIC_KEY_SIZE));
|
2016-12-26 04:37:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the NoSpam value converted to QString.
|
|
|
|
* @return The NoSpam value as QString or "" if the ToxId was constructed from a Public Key.
|
|
|
|
*/
|
|
|
|
QString ToxId::getNoSpamString() const
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
if (toxId.length() == TOX_ADDRESS_SIZE) {
|
2016-12-26 04:37:37 +08:00
|
|
|
return toxId.mid(TOX_PUBLIC_KEY_SIZE, NOSPAM_BYTES).toHex().toUpper();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2015-05-18 02:17:14 +08:00
|
|
|
}
|
2016-11-09 06:57:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
2016-12-28 22:43:11 +08:00
|
|
|
* @note Validates the checksum.
|
|
|
|
*/
|
|
|
|
bool ToxId::isValidToxId(const QString& id)
|
|
|
|
{
|
|
|
|
return isToxId(id) && ToxId(id).isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check, that id is probably a valid Tox ID.
|
|
|
|
* @param id Tox ID to check.
|
|
|
|
* @return True if the string can be a ToxID, false otherwise.
|
|
|
|
* @note Doesn't validate checksum.
|
2016-11-09 06:57:51 +08:00
|
|
|
*/
|
|
|
|
bool ToxId::isToxId(const QString& id)
|
|
|
|
{
|
2016-12-28 22:43:11 +08:00
|
|
|
return id.length() == TOXID_HEX_CHARS && id.contains(ToxIdRegEx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check it it's a valid Tox ID by verifying the checksum
|
|
|
|
* @return True if it is a valid Tox ID, false otherwise.
|
|
|
|
*/
|
|
|
|
bool ToxId::isValid() const
|
|
|
|
{
|
2017-02-26 19:52:45 +08:00
|
|
|
if (toxId.length() != TOX_ADDRESS_SIZE) {
|
2016-11-09 06:57:51 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-28 22:43:11 +08:00
|
|
|
const int size = TOX_PUBLIC_KEY_SIZE + NOSPAM_BYTES;
|
2016-11-09 06:57:51 +08:00
|
|
|
|
2016-12-28 22:43:11 +08:00
|
|
|
QByteArray data = toxId.left(size);
|
|
|
|
QByteArray checksum = toxId.right(CHECKSUM_BYTES);
|
|
|
|
QByteArray calculated(CHECKSUM_BYTES, 0x00);
|
2016-11-09 06:57:51 +08:00
|
|
|
|
2017-02-26 19:52:45 +08:00
|
|
|
for (int i = 0; i < size; i++) {
|
2016-12-28 22:43:11 +08:00
|
|
|
calculated[i % 2] = calculated[i % 2] ^ data[i];
|
2016-11-09 06:57:51 +08:00
|
|
|
}
|
|
|
|
|
2016-12-28 22:43:11 +08:00
|
|
|
return calculated == checksum;
|
|
|
|
}
|