|
|
@ -22,22 +22,12 @@ |
|
|
|
#include "toxid.h" |
|
|
|
#include "toxid.h" |
|
|
|
#include "toxpk.h" |
|
|
|
#include "toxpk.h" |
|
|
|
|
|
|
|
|
|
|
|
#include <tox/tox.h> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <QRegularExpression> |
|
|
|
#include <QRegularExpression> |
|
|
|
|
|
|
|
|
|
|
|
#include <cassert> |
|
|
|
#include <cassert> |
|
|
|
#include <cstdint> |
|
|
|
#include <cstdint> |
|
|
|
|
|
|
|
|
|
|
|
// Tox doesn't publicly define these
|
|
|
|
const QRegularExpression ToxId::ToxIdRegEx(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(ToxId::numHexChars)); |
|
|
|
#define NOSPAM_BYTES 4 |
|
|
|
|
|
|
|
#define CHECKSUM_BYTES 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define NOSPAM_HEX_CHARS (2 * NOSPAM_BYTES) |
|
|
|
|
|
|
|
#define CHECKSUM_HEX_CHARS (2 * CHECKSUM_BYTES) |
|
|
|
|
|
|
|
#define TOXID_HEX_CHARS (2 * TOX_ADDRESS_SIZE) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const QRegularExpression ToxId::ToxIdRegEx(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(TOXID_HEX_CHARS)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @class ToxId |
|
|
|
* @class ToxId |
|
|
@ -112,8 +102,8 @@ ToxId::ToxId(const QByteArray& rawId) |
|
|
|
* If the given rawId isn't a valid Public Key or Tox ID a ToxId with all zero bytes is created. |
|
|
|
* If the given rawId isn't a valid Public Key or Tox ID a ToxId with all zero bytes is created. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param rawId Pointer to bytes to convert to ToxId object |
|
|
|
* @param rawId Pointer to bytes to convert to ToxId object |
|
|
|
* @param len Number of bytes to read. Must be TOX_PUBLIC_KEY_SIZE for a Public Key or |
|
|
|
* @param len Number of bytes to read. Must be ToxPk::size for a Public Key or |
|
|
|
* TOX_ADDRESS_SIZE for a Tox ID. |
|
|
|
* ToxId::size for a Tox ID. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
ToxId::ToxId(const uint8_t* rawId, int len) |
|
|
|
ToxId::ToxId(const uint8_t* rawId, int len) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -124,7 +114,7 @@ ToxId::ToxId(const uint8_t* rawId, int len) |
|
|
|
|
|
|
|
|
|
|
|
void ToxId::constructToxId(const QByteArray& rawId) |
|
|
|
void ToxId::constructToxId(const QByteArray& rawId) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (rawId.length() == TOX_ADDRESS_SIZE && isToxId(rawId.toHex().toUpper())) { |
|
|
|
if (rawId.length() == ToxId::size && isToxId(rawId.toHex().toUpper())) { |
|
|
|
toxId = QByteArray(rawId); // construct from full toxid
|
|
|
|
toxId = QByteArray(rawId); // construct from full toxid
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
assert(!"ToxId constructed with invalid input"); |
|
|
|
assert(!"ToxId constructed with invalid input"); |
|
|
@ -189,7 +179,7 @@ const uint8_t* ToxId::getBytes() const |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
ToxPk ToxId::getPublicKey() const |
|
|
|
ToxPk ToxId::getPublicKey() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto const pkBytes = toxId.left(TOX_PUBLIC_KEY_SIZE); |
|
|
|
auto const pkBytes = toxId.left(ToxPk::size); |
|
|
|
if (pkBytes.isEmpty()) { |
|
|
|
if (pkBytes.isEmpty()) { |
|
|
|
return ToxPk{}; |
|
|
|
return ToxPk{}; |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -203,8 +193,8 @@ ToxPk ToxId::getPublicKey() const |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
QString ToxId::getNoSpamString() const |
|
|
|
QString ToxId::getNoSpamString() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (toxId.length() == TOX_ADDRESS_SIZE) { |
|
|
|
if (toxId.length() == ToxId::size) { |
|
|
|
return toxId.mid(TOX_PUBLIC_KEY_SIZE, NOSPAM_BYTES).toHex().toUpper(); |
|
|
|
return toxId.mid(ToxPk::size, ToxId::nospamSize).toHex().toUpper(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return {}; |
|
|
|
return {}; |
|
|
@ -229,7 +219,7 @@ bool ToxId::isValidToxId(const QString& id) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
bool ToxId::isToxId(const QString& id) |
|
|
|
bool ToxId::isToxId(const QString& id) |
|
|
|
{ |
|
|
|
{ |
|
|
|
return id.length() == TOXID_HEX_CHARS && id.contains(ToxIdRegEx); |
|
|
|
return id.length() == ToxId::numHexChars && id.contains(ToxIdRegEx); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -238,17 +228,17 @@ bool ToxId::isToxId(const QString& id) |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
bool ToxId::isValid() const |
|
|
|
bool ToxId::isValid() const |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (toxId.length() != TOX_ADDRESS_SIZE) { |
|
|
|
if (toxId.length() != ToxId::size) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const int size = TOX_PUBLIC_KEY_SIZE + NOSPAM_BYTES; |
|
|
|
const int pkAndChecksum = ToxPk::size + ToxId::nospamSize; |
|
|
|
|
|
|
|
|
|
|
|
QByteArray data = toxId.left(size); |
|
|
|
QByteArray data = toxId.left(pkAndChecksum); |
|
|
|
QByteArray checksum = toxId.right(CHECKSUM_BYTES); |
|
|
|
QByteArray checksum = toxId.right(ToxId::checksumSize); |
|
|
|
QByteArray calculated(CHECKSUM_BYTES, 0x00); |
|
|
|
QByteArray calculated(ToxId::checksumSize, 0x00); |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < size; i++) { |
|
|
|
for (int i = 0; i < pkAndChecksum; i++) { |
|
|
|
calculated[i % 2] = calculated[i % 2] ^ data[i]; |
|
|
|
calculated[i % 2] = calculated[i % 2] ^ data[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|