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

refactor(toxpk): add class representing a Tox Public Key

This commit is contained in:
sudden6 2016-12-29 17:09:20 +01:00
parent b5af649190
commit 2f4e8dc3e8
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
3 changed files with 134 additions and 0 deletions

View File

@ -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 \

106
src/core/toxpk.cpp Normal file
View File

@ -0,0 +1,106 @@
#include "toxpk.h"
#include <tox/tox.h>
#include <QByteArray>
#include <QString>
/**
* @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<const char*>(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<const uint8_t*>(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();
}

26
src/core/toxpk.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef TOXPK_H
#define TOXPK_H
#include <cstdint>
#include <QByteArray>
#include <QByteArray>
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