From a432a16e018913d516e8004bc9c48fb3c58c5556 Mon Sep 17 00:00:00 2001 From: apprb Date: Sat, 8 Nov 2014 23:28:19 +0900 Subject: [PATCH] encrypted history: class constructor: refacroring --- src/core.cpp | 7 ++++-- src/misc/db/encrypteddb.cpp | 44 +++++++++++++++++++++++-------------- src/misc/db/encrypteddb.h | 3 ++- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/core.cpp b/src/core.cpp index e5c5f611c..58937a51f 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -1811,7 +1811,8 @@ QByteArray Core::decryptData(const QByteArray& data, PasswordType passtype) return QByteArray(); int sz = data.size() - tox_pass_encryption_extra_length(); uint8_t decrypted[sz]; - if (tox_pass_key_decrypt(reinterpret_cast(data.data()), data.size(), pwsaltedkeys[passtype], decrypted) != sz) + int decr_size = tox_pass_key_decrypt(reinterpret_cast(data.data()), data.size(), pwsaltedkeys[passtype], decrypted); + if (decr_size != sz) { qWarning() << "Core::decryptData: decryption failed"; return QByteArray(); @@ -1907,5 +1908,7 @@ QByteArray Core::getSaltFromFile(QString filename) return QByteArray(); } - return QByteArray::fromRawData(reinterpret_cast(salt), tox_pass_salt_length()); + QByteArray res = QByteArray::fromRawData(reinterpret_cast(salt), tox_pass_salt_length()); + qDebug() << res.toBase64(); + return res; } diff --git a/src/misc/db/encrypteddb.cpp b/src/misc/db/encrypteddb.cpp index 43f69a5d3..8ae7ea123 100644 --- a/src/misc/db/encrypteddb.cpp +++ b/src/misc/db/encrypteddb.cpp @@ -28,24 +28,32 @@ qint64 EncryptedDb::plainChunkSize = 4096; qint64 EncryptedDb::encryptedChunkSize = EncryptedDb::plainChunkSize + tox_pass_encryption_extra_length(); EncryptedDb::EncryptedDb(const QString &fname, QList initList) : - PlainDb(":memory:", initList), encrFile(fname) + PlainDb(":memory:", initList), fileName(fname) { QByteArray fileContent; - if (pullFileContent()) + if (pullFileContent(fileName, buffer)) { chunkPosition = encrFile.size() / encryptedChunkSize; - encrFile.seek(0); + qDebug() << "writing old data"; + encrFile.setFileName(fileName); + encrFile.open(QIODevice::ReadOnly); fileContent = encrFile.readAll(); + encrFile.close(); } else { qWarning() << "corrupted history log file will be wiped!"; chunkPosition = 0; } - encrFile.close(); - encrFile.open(QIODevice::WriteOnly); - encrFile.write(fileContent); - encrFile.flush(); + encrFile.setFileName(fileName); + + if (!encrFile.open(QIODevice::WriteOnly)) + { + qWarning() << "can't open file:" << fileName; + } else { + encrFile.write(fileContent); + encrFile.flush(); + } } EncryptedDb::~EncryptedDb() @@ -63,23 +71,25 @@ QSqlQuery EncryptedDb::exec(const QString &query) return retQSqlQuery; } -bool EncryptedDb::pullFileContent() +bool EncryptedDb::pullFileContent(const QString &fname, QByteArray &buf) { qDebug() << "EncryptedDb::pullFileContent()"; - encrFile.open(QIODevice::ReadOnly); + + QFile dbFile(fname); + dbFile.open(QIODevice::ReadOnly); QByteArray fileContent; - while (!encrFile.atEnd()) + while (!dbFile.atEnd()) { - QByteArray encrChunk = encrFile.read(encryptedChunkSize); + QByteArray encrChunk = dbFile.read(encryptedChunkSize); qDebug() << "got chunk:" << encrChunk.size(); - buffer = Core::getInstance()->decryptData(encrChunk, Core::ptHistory); - if (buffer.size() > 0) + buf = Core::getInstance()->decryptData(encrChunk, Core::ptHistory); + if (buf.size() > 0) { - fileContent += buffer; + fileContent += buf; } else { qWarning() << "Encrypted history log is corrupted: can't decrypt"; - buffer = QByteArray(); + buf = QByteArray(); return false; } } @@ -106,7 +116,7 @@ bool EncryptedDb::pullFileContent() if (!isGoodLine) { qWarning() << "Encrypted history log is corrupted: errors in content"; - buffer = QByteArray(); + buf = QByteArray(); return false; } } @@ -116,6 +126,8 @@ bool EncryptedDb::pullFileContent() QSqlQuery r = PlainDb::exec(line); } + dbFile.close(); + return true; } diff --git a/src/misc/db/encrypteddb.h b/src/misc/db/encrypteddb.h index 29948c2b4..bbac817c4 100644 --- a/src/misc/db/encrypteddb.h +++ b/src/misc/db/encrypteddb.h @@ -32,10 +32,11 @@ public: static bool check(const QString &fname); private: - bool pullFileContent(); + bool pullFileContent(const QString& fname, QByteArray &buf); void appendToEncrypted(const QString &sql); QFile encrFile; + QString fileName; static qint64 plainChunkSize; static qint64 encryptedChunkSize;