qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
toxid.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2015-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #include "contactid.h"
22 #include "toxid.h"
23 #include "toxpk.h"
24 
25 #include <tox/tox.h>
26 
27 #include <QRegularExpression>
28 
29 #include <cassert>
30 #include <cstdint>
31 
32 // Tox doesn't publicly define these
33 #define NOSPAM_BYTES 4
34 #define CHECKSUM_BYTES 2
35 
36 #define NOSPAM_HEX_CHARS (2 * NOSPAM_BYTES)
37 #define CHECKSUM_HEX_CHARS (2 * CHECKSUM_BYTES)
38 #define TOXID_HEX_CHARS (2 * TOX_ADDRESS_SIZE)
39 
40 const QRegularExpression ToxId::ToxIdRegEx(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(TOXID_HEX_CHARS));
41 
63  : toxId()
64 {}
65 
70 ToxId::ToxId(const ToxId& other)
71  : toxId(other.toxId)
72 {}
73 
83 ToxId::ToxId(const QString& id)
84 {
85  if (isToxId(id)) {
86  toxId = QByteArray::fromHex(id.toLatin1());
87  } else {
88  assert(!"ToxId constructed with invalid length string");
89  toxId = QByteArray(); // invalid id string
90  }
91 }
92 
102 ToxId::ToxId(const QByteArray& rawId)
103 {
104  constructToxId(rawId);
105 }
106 
118 ToxId::ToxId(const uint8_t* rawId, int len)
119 {
120  QByteArray tmpId(reinterpret_cast<const char*>(rawId), len);
121  constructToxId(tmpId);
122 }
123 
124 
125 void ToxId::constructToxId(const QByteArray& rawId)
126 {
127  if (rawId.length() == TOX_ADDRESS_SIZE && isToxId(rawId.toHex().toUpper())) {
128  toxId = QByteArray(rawId); // construct from full toxid
129  } else {
130  assert(!"ToxId constructed with invalid input");
131  toxId = QByteArray(); // invalid id
132  }
133 }
134 
140 bool ToxId::operator==(const ToxId& other) const
141 {
142  return getPublicKey() == other.getPublicKey();
143 }
144 
150 bool ToxId::operator!=(const ToxId& other) const
151 {
152  return getPublicKey() != other.getPublicKey();
153 }
154 
160 QString ToxId::toString() const
161 {
162  return toxId.toHex().toUpper();
163 }
164 
169 {
170  toxId.clear();
171 }
172 
177 const uint8_t* ToxId::getBytes() const
178 {
179  if (isValid()) {
180  return reinterpret_cast<const uint8_t*>(toxId.constData());
181  }
182 
183  return nullptr;
184 }
185 
191 {
192  auto const pkBytes = toxId.left(TOX_PUBLIC_KEY_SIZE);
193  if (pkBytes.isEmpty()) {
194  return ToxPk{};
195  } else {
196  return ToxPk{pkBytes};
197  }
198 }
199 
204 QString ToxId::getNoSpamString() const
205 {
206  if (toxId.length() == TOX_ADDRESS_SIZE) {
207  return toxId.mid(TOX_PUBLIC_KEY_SIZE, NOSPAM_BYTES).toHex().toUpper();
208  }
209 
210  return {};
211 }
212 
219 bool ToxId::isValidToxId(const QString& id)
220 {
221  return isToxId(id) && ToxId(id).isValid();
222 }
223 
230 bool ToxId::isToxId(const QString& id)
231 {
232  return id.length() == TOXID_HEX_CHARS && id.contains(ToxIdRegEx);
233 }
234 
239 bool ToxId::isValid() const
240 {
241  if (toxId.length() != TOX_ADDRESS_SIZE) {
242  return false;
243  }
244 
245  const int size = TOX_PUBLIC_KEY_SIZE + NOSPAM_BYTES;
246 
247  QByteArray data = toxId.left(size);
248  QByteArray checksum = toxId.right(CHECKSUM_BYTES);
249  QByteArray calculated(CHECKSUM_BYTES, 0x00);
250 
251  for (int i = 0; i < size; i++) {
252  calculated[i % 2] = calculated[i % 2] ^ data[i];
253  }
254 
255  return calculated == checksum;
256 }
ToxId::operator!=
bool operator!=(const ToxId &other) const
Compares the inequality of the Public Key.
Definition: toxid.cpp:150
ToxId::toString
QString toString() const
Returns the Tox ID converted to QString. Is equal to getPublicKey() if the Tox ID was constructed fro...
Definition: toxid.cpp:160
toxid.h
ToxId::getNoSpamString
QString getNoSpamString() const
Returns the NoSpam value converted to QString.
Definition: toxid.cpp:204
ToxId::constructToxId
void constructToxId(const QByteArray &rawId)
Definition: toxid.cpp:125
ToxId::clear
void clear()
Clears all elements of the Tox ID.
Definition: toxid.cpp:168
ToxId::ToxIdRegEx
static const QRegularExpression ToxIdRegEx
Definition: toxid.h:57
ToxId::isValid
bool isValid() const
Check it it's a valid Tox ID by verifying the checksum.
Definition: toxid.cpp:239
ToxId::getBytes
const uint8_t * getBytes() const
Gets the ToxID as bytes, convenience function for toxcore interface.
Definition: toxid.cpp:177
ToxId::ToxId
ToxId()
The default constructor. Creates an empty Tox ID.
Definition: toxid.cpp:62
CHECKSUM_BYTES
#define CHECKSUM_BYTES
Definition: toxid.cpp:34
toxpk.h
NOSPAM_BYTES
#define NOSPAM_BYTES
Definition: toxid.cpp:33
contactid.h
ToxId::isToxId
static bool isToxId(const QString &id)
Check, that id is probably a valid Tox ID.
Definition: toxid.cpp:230
ToxId::getPublicKey
ToxPk getPublicKey() const
Gets the Public Key part of the ToxID.
Definition: toxid.cpp:190
ToxPk
This class represents a Tox Public Key, which is a part of Tox ID.
Definition: toxpk.h:26
ToxId::toxId
QByteArray toxId
Definition: toxid.h:60
ToxId
This class represents a Tox ID.
Definition: toxid.h:29
TOXID_HEX_CHARS
#define TOXID_HEX_CHARS
Definition: toxid.cpp:38
ToxId::operator==
bool operator==(const ToxId &other) const
Compares the equality of the Public Key.
Definition: toxid.cpp:140
ToxId::isValidToxId
static bool isValidToxId(const QString &id)
Check, that id is a valid Tox ID.
Definition: toxid.cpp:219