From 2f4e8dc3e8563fe6a9e4f5fa306c44aaf411ef71 Mon Sep 17 00:00:00 2001 From: sudden6 Date: Thu, 29 Dec 2016 17:09:20 +0100 Subject: [PATCH] refactor(toxpk): add class representing a Tox Public Key --- qtox.pro | 2 + src/core/toxpk.cpp | 106 +++++++++++++++++++++++++++++++++++++++++++++ src/core/toxpk.h | 26 +++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/core/toxpk.cpp create mode 100644 src/core/toxpk.h diff --git a/qtox.pro b/qtox.pro index 35031795d..fa8f190b6 100644 --- a/qtox.pro +++ b/qtox.pro @@ -435,6 +435,7 @@ HEADERS += \ src/core/recursivesignalblocker.h \ src/core/toxcall.h \ src/core/toxid.h \ + src/core/toxpk.h \ src/friend.h \ src/friendlist.h \ src/group.h \ @@ -554,6 +555,7 @@ SOURCES += \ src/core/recursivesignalblocker.cpp \ src/core/toxcall.cpp \ src/core/toxid.cpp \ + src/core/toxpk.cpp \ src/friend.cpp \ src/friendlist.cpp \ src/group.cpp \ diff --git a/src/core/toxpk.cpp b/src/core/toxpk.cpp new file mode 100644 index 000000000..42011c2f9 --- /dev/null +++ b/src/core/toxpk.cpp @@ -0,0 +1,106 @@ +#include "toxpk.h" + +#include + +#include +#include + +/** + * @class ToxPk + * @brief This class represents a Tox Public Key, which is a part of Tox ID. + */ + +/** + * @brief The default constructor. Creates an empty Tox key. + */ +ToxPk::ToxPk() +: key() +{} + +/** + * @brief The copy constructor. + * @param other ToxKey to copy + */ +ToxPk::ToxPk(const ToxPk& other) +: key(other.key) +{} + +/** + * @brief Constructs a ToxKey from bytes. + * @param rawId The bytes to construct the ToxKey from. The lenght must be exactly + * TOX_PUBLIC_KEY_SIZE, else the ToxKey will be empty. + */ +ToxPk::ToxPk(const QByteArray& rawId) +{ + if(rawId.length() == TOX_PUBLIC_KEY_SIZE) + { + key = QByteArray(rawId); + } + else + { + key = QByteArray(); + } +} + +/** + * @brief Constructs a ToxKey from bytes. + * @param rawId The bytes to construct the ToxKey from, will read exactly + * TOX_PUBLIC_KEY_SIZE from the specified buffer. + */ +ToxPk::ToxPk(const uint8_t* rawId) +{ + key = QByteArray(reinterpret_cast(rawId), TOX_PUBLIC_KEY_SIZE); +} + +/** + * @brief Compares the equality of the ToxKey. + * @param other ToxKey to compare. + * @return True if both ToxKeys are equal, false otherwise. + */ +bool ToxPk::operator==(const ToxPk& other) const +{ + return key == other.key; +} + +/** + * @brief Compares the inequality of the ToxKey. + * @param other ToxKey to compare. + * @return True if both ToxKeys are not equal, false otherwise. + */ +bool ToxPk::operator!=(const ToxPk& other) const +{ + return key != other.key; +} + +/** + * @brief Converts the ToxKey to a uppercase hex string. + * @return QString containing the hex representation of the key + */ +QString ToxPk::toString() const +{ + return key.toHex().toUpper(); +} + +/** + * @brief Returns a pointer to the raw key data. + * @return Pointer to the raw key data, which is exactly TOX_PUBLIC_KEY_SIZE bytes + * long. Returns a nullptr if the ToxKey is empty. + */ +const uint8_t* ToxPk::getBytes() const +{ + if(key.isEmpty()) + { + return nullptr; + } + + return reinterpret_cast(key.constData()); +} + +/** + * @brief Checks if the ToxKey contains a key. + * @return True if there is a key, False otherwise. + */ +bool ToxPk::isEmpty() const +{ + return key.isEmpty(); +} diff --git a/src/core/toxpk.h b/src/core/toxpk.h new file mode 100644 index 000000000..e05c95e41 --- /dev/null +++ b/src/core/toxpk.h @@ -0,0 +1,26 @@ +#ifndef TOXPK_H +#define TOXPK_H + +#include +#include +#include + +class ToxPk +{ +public: + ToxPk(); + ToxPk(const ToxPk& other); + explicit ToxPk(const QByteArray& rawId); + explicit ToxPk(const uint8_t* rawId); + + bool operator==(const ToxPk& other) const; + bool operator!=(const ToxPk& other) const; + QString toString() const; + const uint8_t* getBytes() const; + bool isEmpty() const; + +private: + QByteArray key; +}; + +#endif // TOXPK_H