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

Merge branch 'history_fx' into history

Conflicts:
	src/misc/db/encrypteddb.cpp
	src/misc/db/encrypteddb.h
This commit is contained in:
apprb 2014-10-17 22:14:27 +09:00
commit 177b27882b
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
3 changed files with 73 additions and 26 deletions

View File

@ -30,22 +30,25 @@ EncryptedDb::EncryptedDb(const QString &fname) :
plainChunkSize = 1024;
encryptedChunkSize = plainChunkSize + tox_pass_encryption_extra_length();
encrFile.open(QIODevice::ReadOnly);
QList<QString> sqlCommands = decryptFile();
for (const QString &cmd : sqlCommands)
QByteArray fileContent;
if (pullFileContent())
{
// check line here
QSqlQuery r = PlainDb::exec(cmd);
qDebug() << r.lastError();
chunkPosition = encrFile.size() / encryptedChunkSize;
encrFile.seek(0);
fileContent = encrFile.readAll();
/*
if (encrFile.size() > 0)
{
encrFile.copy(fname + "~");
}
*/
} else {
qWarning() << "corrupted history log file will be wiped!";
chunkPosition = 0;
}
chunkPosition = encrFile.size() / encryptedChunkSize;
// encrFile.seek(chunkPosition * encryptedChunkSize);
// buffer = encrFile.read(encrFile.size() % encryptedChunkSize);
encrFile.seek(0);
QByteArray fileContent = encrFile.readAll();
encrFile.close();
encrFile.open(QIODevice::WriteOnly);
encrFile.write(fileContent);
@ -70,28 +73,59 @@ bool EncryptedDb::save()
return true;
}
QList<QString> EncryptedDb::decryptFile()
bool EncryptedDb::pullFileContent()
{
encrFile.open(QIODevice::ReadOnly);
QByteArray fileContent;
while (!encrFile.atEnd())
{
QByteArray encrChunk = encrFile.read(encryptedChunkSize);
buffer = Core::getInstance()->decryptData(encrChunk);
fileContent += buffer;
if (buffer.size() > 0)
{
fileContent += buffer;
} else {
qWarning() << "Encrypted history log is corrupted: can't decrypt";
buffer = QByteArray();
return false;
}
}
QList<QByteArray> splittedBA = fileContent.split('\n');
QList<QString> res;
QList<QString> sqlCmds;
for (auto ba_line : splittedBA)
{
QString line = QByteArray::fromBase64(ba_line);
//check line correctness here
res.append(line);
// res.append(ba_line);
if (line.size() == 0)
continue;
bool isGoodLine = false;
if (line.startsWith("CREATE", Qt::CaseInsensitive) || line.startsWith("INSERT", Qt::CaseInsensitive))
{
if (line.endsWith(");"))
{
sqlCmds.append(line);
isGoodLine = true;
}
}
if (!isGoodLine)
{
qWarning() << "Encrypted history log is corrupted: errors in content";
buffer = QByteArray();
return false;
}
}
return res;
for (auto line : sqlCmds)
{
QSqlQuery r = PlainDb::exec(line);
qDebug() << r.lastError();
}
return true;
}
void EncryptedDb::appendToEncrypted(const QString &sql)
@ -106,14 +140,21 @@ void EncryptedDb::appendToEncrypted(const QString &sql)
{
QByteArray filledChunk = buffer.left(plainChunkSize);
encrFile.seek(chunkPosition * encryptedChunkSize);
encrFile.write(Core::getInstance()->encryptData(filledChunk));
QByteArray encr = Core::getInstance()->encryptData(filledChunk);
if (encr.size() > 0)
{
encrFile.write(encr);
}
buffer = buffer.right(buffer.size() - plainChunkSize);
chunkPosition++;
}
encrFile.seek(chunkPosition * encryptedChunkSize);
encrFile.write(Core::getInstance()->encryptData(buffer));
QByteArray encr = Core::getInstance()->encryptData(buffer);
if (encr.size() > 0)
{
encrFile.write(encr);
}
encrFile.flush();
qDebug() << sql;
}

View File

@ -32,7 +32,7 @@ public:
virtual bool save();
private:
QList<QString> decryptFile();
bool pullFileContent();
void appendToEncrypted(const QString &sql);
QFile encrFile;

View File

@ -37,7 +37,7 @@
<item>
<widget class="QGroupBox" name="encryptionGroup">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="title">
<string>Encryption</string>
@ -45,6 +45,9 @@
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QCheckBox" name="cbEncryptTox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Encrypt Tox datafile</string>
</property>
@ -74,6 +77,9 @@
</item>
<item>
<widget class="QPushButton" name="pswdBtn">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Set profile password</string>
</property>