Skip to content
This repository has been archived by the owner on Nov 2, 2019. It is now read-only.

Commit

Permalink
Refactored libsparkle to use pimpl
Browse files Browse the repository at this point in the history
  • Loading branch information
grindars committed Jul 19, 2010
1 parent 096da20 commit ad7e9d7
Show file tree
Hide file tree
Showing 19 changed files with 620 additions and 306 deletions.
84 changes: 70 additions & 14 deletions libsparkle/BlowfishKey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,26 @@ blowfish_get_info(int algo, size_t *keylen, size_t *blocksize, size_t *contextsi
void (**r_encrypt)(void *c, quint8 *outbuf, const quint8 *inbuf),
void (**r_decrypt)( void *c, quint8 *outbuf, const quint8 *inbuf));

BlowfishKey::BlowfishKey(QObject *parent) : QObject(parent)
{
class BlowfishKeyPrivate {
public:
BlowfishKeyPrivate();

void generate();
void setBytes(const QByteArray &raw);
QByteArray encrypt(QByteArray data) const;
QByteArray decrypt(QByteArray data) const;

void *key;
QByteArray rawKey;

size_t keylen, blocksize, contextsize;

int (*cb_setkey)(void *c, const quint8 *key, unsigned keylen);
void (*cb_encrypt)(void *c, quint8 *outbuf, const quint8 *inbuf);
void (*cb_decrypt)(void *c, quint8 *outbuf, const quint8 *inbuf);
};

BlowfishKeyPrivate::BlowfishKeyPrivate() {
if(blowfish_get_info(4, &keylen, &blocksize, &contextsize, &cb_setkey, &cb_encrypt, &cb_decrypt) == NULL)
Log::fatal("blowfish_get_info failed");

Expand All @@ -38,28 +56,21 @@ BlowfishKey::BlowfishKey(QObject *parent) : QObject(parent)
key = malloc(contextsize);
}

BlowfishKey::~BlowfishKey() {
free(key);
}

void BlowfishKey::generate() {
void BlowfishKeyPrivate::generate() {
rawKey.resize(32);
random_bytes((unsigned char *) rawKey.data(), rawKey.size());

cb_setkey(key, (unsigned char *) rawKey.data(), rawKey.size());
}

QByteArray BlowfishKey::bytes() const {
return rawKey;
}

void BlowfishKey::setBytes(QByteArray raw) {
void BlowfishKeyPrivate::setBytes(const QByteArray &raw) {
rawKey = raw;

cb_setkey(key, (unsigned char *) rawKey.data(), rawKey.size());
}

QByteArray BlowfishKey::encrypt(QByteArray data) const {
QByteArray BlowfishKeyPrivate::encrypt(QByteArray data) const {

unsigned char *chunk = new unsigned char[blocksize];

QByteArray output;
Expand All @@ -78,7 +89,7 @@ QByteArray BlowfishKey::encrypt(QByteArray data) const {
return output;
}

QByteArray BlowfishKey::decrypt(QByteArray data) const {
QByteArray BlowfishKeyPrivate::decrypt(QByteArray data) const {
unsigned char *chunk = new unsigned char[blocksize];

QByteArray output;
Expand All @@ -94,3 +105,48 @@ QByteArray BlowfishKey::decrypt(QByteArray data) const {
return output;
}

BlowfishKey::BlowfishKey(BlowfishKeyPrivate &dd, QObject *parent) : QObject(parent), d_ptr(&dd)
{

}

BlowfishKey::BlowfishKey(QObject *parent) : QObject(parent), d_ptr(new BlowfishKeyPrivate)
{

}

BlowfishKey::~BlowfishKey() {
delete d_ptr;
}

void BlowfishKey::generate() {
Q_D(BlowfishKey);

d->generate();
}

QByteArray BlowfishKey::bytes() const {
Q_D(const BlowfishKey);

return d->rawKey;
}

void BlowfishKey::setBytes(QByteArray raw) {
Q_D(BlowfishKey);

d->setBytes(raw);
}

QByteArray BlowfishKey::encrypt(QByteArray data) const {
Q_D(const BlowfishKey);

return d->encrypt(data);
}


QByteArray BlowfishKey::decrypt(QByteArray data) const {
Q_D(const BlowfishKey);

return d->decrypt(data);
}

2 changes: 1 addition & 1 deletion libsparkle/LinkLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
#include "LinkLayer.h"
#include "SparkleNode.h"
#include "PacketTransport.h"
#include "SHA1Digest.h"
#include "Router.h"
#include "Log.h"
#include "ApplicationLayer.h"
#include "BlowfishKey.h"

LinkLayer::LinkLayer(Router &router, PacketTransport &_transport, RSAKeyPair &_hostKeyPair)
: QObject(NULL), hostKeyPair(_hostKeyPair), _router(router), transport(_transport), joined(false), preparingForShutdown(false)
Expand Down
77 changes: 77 additions & 0 deletions libsparkle/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <cstdlib>

#include "Log.h"
#include "SparkleNode.h"

QString Log::prepare() {
QString imm = stream->format;
Expand Down Expand Up @@ -58,3 +59,79 @@ void Log::emitMessage(loglevel_t loglevel, QString message) {
}
}

Log& Log::operator<<(short v) {
stream->list.append(QString::number(v, stream->base));

return *this;
}

Log& Log::operator<<(ushort v) {
stream->list.append(QString::number(v, stream->base));

return *this;
}

Log& Log::operator<<(int v) {
stream->list.append(QString::number(v, stream->base));

return *this;
}

Log& Log::operator<<(uint v) {
stream->list.append(QString::number(v, stream->base));

return *this;
}

Log& Log::operator<<(long v) {
stream->list.append(QString::number(v, stream->base));

return *this;
}

Log& Log::operator<<(ulong v) {
stream->list.append(QString::number(v, stream->base));

return *this;
}

Log& Log::operator<<(double v) {
stream->list.append(QString::number(v, 'g', 4));

return *this;
}

Log& Log::operator<<(char v) {
stream->list.append(QString(v));

return *this;
}

Log& Log::operator<<(const char* v) {
stream->list.append(v);

return *this;
}

Log& Log::operator<<(bool v) {
stream->list.append(v ? "true" : "false");

return *this;
}

Log& Log::operator<<(const QString &v) {
stream->list.append(v);

return *this;
}

Log& Log::operator<<(const QHostAddress &v) {
stream->list.append(v.toString());

return *this;
}

Log& Log::operator<<(const SparkleNode &v) {
return *this << v.realIP().toString() << v.realPort();
}

Loading

0 comments on commit ad7e9d7

Please sign in to comment.