diff --git a/src/historykeeper.cpp b/src/historykeeper.cpp index 4e0cca13c..446e44255 100644 --- a/src/historykeeper.cpp +++ b/src/historykeeper.cpp @@ -44,9 +44,8 @@ HistoryKeeper *HistoryKeeper::getInstance() if (encrypted) { - QString key = "plainKey"; // FIXME: ask user about it path = QDir(Settings::getInstance().getSettingsDirPath()).filePath("qtox_history.encrypted"); - dbIntf = new EncryptedDb(path, key); + dbIntf = new EncryptedDb(path); historyInstance = new HistoryKeeper(dbIntf); return historyInstance; diff --git a/src/misc/db/encrypteddb.cpp b/src/misc/db/encrypteddb.cpp index c83605501..6240b0420 100644 --- a/src/misc/db/encrypteddb.cpp +++ b/src/misc/db/encrypteddb.cpp @@ -16,6 +16,7 @@ #include "encrypteddb.h" #include "src/misc/settings.h" +#include "src/core.h" #include @@ -23,17 +24,9 @@ #include #include -EncryptedDb::EncryptedDb(const QString &fname, const QString &key) : +EncryptedDb::EncryptedDb(const QString &fname) : PlainDb(":memory:"), encrFile(fname) { - encrkey = new u_int8_t[tox_pass_key_length()]; - QByteArray key_ba; - key_ba.append(key); -// tox_derive_key_from_pass(reinterpret_cast(key_ba.data()), key_ba.size(), encrkey); - passwd = "test"; - - qDebug() << QByteArray::fromRawData(reinterpret_cast(encrkey), tox_pass_key_length()).toBase64(); - plainChunkSize = 1024; encryptedChunkSize = plainChunkSize + tox_pass_encryption_extra_length(); @@ -60,8 +53,7 @@ EncryptedDb::EncryptedDb(const QString &fname, const QString &key) : EncryptedDb::~EncryptedDb() { - encrFile.close(); - delete encrkey; + encrFile.close(); // what if program is killed without being able to clean up? } QSqlQuery EncryptedDb::exec(const QString &query) @@ -85,7 +77,7 @@ QList EncryptedDb::decryptFile() while (!encrFile.atEnd()) { QByteArray encrChunk = encrFile.read(encryptedChunkSize); - buffer = decrypt(encrChunk); + buffer = Core::getInstance()->decryptData(encrChunk); fileContent += buffer; } @@ -114,44 +106,14 @@ void EncryptedDb::appendToEncrypted(const QString &sql) { QByteArray filledChunk = buffer.left(plainChunkSize); encrFile.seek(chunkPosition * encryptedChunkSize); - encrFile.write(encrypt(filledChunk)); + encrFile.write(Core::getInstance()->encryptData(filledChunk)); buffer = buffer.right(buffer.size() - plainChunkSize); chunkPosition++; } encrFile.seek(chunkPosition * encryptedChunkSize); - encrFile.write(encrypt(buffer)); + encrFile.write(Core::getInstance()->encryptData(buffer)); encrFile.flush(); qDebug() << sql; } - -QByteArray EncryptedDb::encrypt(QByteArray data) -{ - int encrSize = data.size() + tox_pass_encryption_extra_length(); - int plainSize = data.size(); - - uint8_t *out = new u_int8_t[encrSize]; -// int state = tox_pass_key_encrypt(reinterpret_cast(data.data()), plainSize, encrkey, out); - int state = tox_pass_encrypt(reinterpret_cast(data.data()), plainSize, - reinterpret_cast(passwd.data()), passwd.size(), out); - qDebug() << state; - - QByteArray ret = QByteArray::fromRawData(reinterpret_cast(out), encrSize); - return ret; -} - -QByteArray EncryptedDb::decrypt(QByteArray data) -{ - int encrSize = data.size(); - int plainSize = data.size() - tox_pass_encryption_extra_length(); - - uint8_t *out = new u_int8_t[plainSize]; -// int state = tox_pass_key_decrypt(reinterpret_cast(data.data()), encrSize, encrkey, out); - int state = tox_pass_decrypt(reinterpret_cast(data.data()), encrSize, - reinterpret_cast(passwd.data()), passwd.size(), out); - qDebug() << state << encrSize << plainSize; - - QByteArray ret = QByteArray::fromRawData(reinterpret_cast(out), plainSize); - return ret; -} diff --git a/src/misc/db/encrypteddb.h b/src/misc/db/encrypteddb.h index 790c909b7..11c999f30 100644 --- a/src/misc/db/encrypteddb.h +++ b/src/misc/db/encrypteddb.h @@ -25,22 +25,17 @@ class EncryptedDb : public PlainDb { public: - EncryptedDb(const QString& fname, const QString &key); + EncryptedDb(const QString& fname); virtual ~EncryptedDb(); virtual QSqlQuery exec(const QString &query); virtual bool save(); private: - QByteArray encrypt(QByteArray data); - QByteArray decrypt(QByteArray data); - QList decryptFile(); void appendToEncrypted(const QString &sql); - u_int8_t *encrkey; QFile encrFile; - QByteArray passwd; qint64 plainChunkSize; qint64 encryptedChunkSize;