1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
This commit is contained in:
apprb 2014-10-17 21:33:00 +09:00
parent 1b9eb3d239
commit 1ce4820aa4
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
3 changed files with 92 additions and 31 deletions

View File

@ -37,22 +37,25 @@ EncryptedDb::EncryptedDb(const QString &fname, const QString &key) :
plainChunkSize = 1024; plainChunkSize = 1024;
encryptedChunkSize = plainChunkSize + tox_pass_encryption_extra_length(); encryptedChunkSize = plainChunkSize + tox_pass_encryption_extra_length();
encrFile.open(QIODevice::ReadOnly); QByteArray fileContent;
if (pullFileContent())
QList<QString> sqlCommands = decryptFile();
for (const QString &cmd : sqlCommands)
{ {
// check line here chunkPosition = encrFile.size() / encryptedChunkSize;
QSqlQuery r = PlainDb::exec(cmd);
qDebug() << r.lastError(); 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.close();
encrFile.open(QIODevice::WriteOnly); encrFile.open(QIODevice::WriteOnly);
encrFile.write(fileContent); encrFile.write(fileContent);
@ -78,28 +81,59 @@ bool EncryptedDb::save()
return true; return true;
} }
QList<QString> EncryptedDb::decryptFile() bool EncryptedDb::pullFileContent()
{ {
encrFile.open(QIODevice::ReadOnly);
QByteArray fileContent; QByteArray fileContent;
while (!encrFile.atEnd()) while (!encrFile.atEnd())
{ {
QByteArray encrChunk = encrFile.read(encryptedChunkSize); QByteArray encrChunk = encrFile.read(encryptedChunkSize);
buffer = decrypt(encrChunk); buffer = decrypt(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<QByteArray> splittedBA = fileContent.split('\n');
QList<QString> res; QList<QString> sqlCmds;
for (auto ba_line : splittedBA) for (auto ba_line : splittedBA)
{ {
QString line = QByteArray::fromBase64(ba_line); QString line = QByteArray::fromBase64(ba_line);
//check line correctness here if (line.size() == 0)
res.append(line); continue;
// res.append(ba_line);
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) void EncryptedDb::appendToEncrypted(const QString &sql)
@ -114,14 +148,23 @@ void EncryptedDb::appendToEncrypted(const QString &sql)
{ {
QByteArray filledChunk = buffer.left(plainChunkSize); QByteArray filledChunk = buffer.left(plainChunkSize);
encrFile.seek(chunkPosition * encryptedChunkSize); encrFile.seek(chunkPosition * encryptedChunkSize);
encrFile.write(encrypt(filledChunk));
QByteArray encr = encrypt(filledChunk);
if (encr.size() > 0)
{
encrFile.write(encr);
}
buffer = buffer.right(buffer.size() - plainChunkSize); buffer = buffer.right(buffer.size() - plainChunkSize);
chunkPosition++; chunkPosition++;
} }
encrFile.seek(chunkPosition * encryptedChunkSize); encrFile.seek(chunkPosition * encryptedChunkSize);
encrFile.write(encrypt(buffer)); QByteArray encr = encrypt(buffer);
encrFile.flush(); if (encr.size() > 0)
{
encrFile.write(encrypt(buffer));
encrFile.flush();
}
qDebug() << sql; qDebug() << sql;
} }
@ -132,10 +175,16 @@ QByteArray EncryptedDb::encrypt(QByteArray data)
int plainSize = data.size(); int plainSize = data.size();
uint8_t *out = new u_int8_t[encrSize]; uint8_t *out = new u_int8_t[encrSize];
// int state = tox_pass_key_encrypt(reinterpret_cast<uint8_t*>(data.data()), plainSize, encrkey, out); //int state = tox_pass_key_encrypt(reinterpret_cast<uint8_t*>(data.data()), plainSize, encrkey, out);
int state = tox_pass_encrypt(reinterpret_cast<uint8_t*>(data.data()), plainSize, int state = tox_pass_encrypt(reinterpret_cast<uint8_t*>(data.data()), plainSize,
reinterpret_cast<uint8_t*>(passwd.data()), passwd.size(), out); reinterpret_cast<uint8_t*>(passwd.data()), passwd.size(), out);
qDebug() << state;
if (state == -1)
{
qWarning() << "encryption failed!";
delete out;
return QByteArray();
}
QByteArray ret = QByteArray::fromRawData(reinterpret_cast<const char*>(out), encrSize); QByteArray ret = QByteArray::fromRawData(reinterpret_cast<const char*>(out), encrSize);
return ret; return ret;
@ -147,10 +196,16 @@ QByteArray EncryptedDb::decrypt(QByteArray data)
int plainSize = data.size() - tox_pass_encryption_extra_length(); int plainSize = data.size() - tox_pass_encryption_extra_length();
uint8_t *out = new u_int8_t[plainSize]; uint8_t *out = new u_int8_t[plainSize];
// int state = tox_pass_key_decrypt(reinterpret_cast<uint8_t*>(data.data()), encrSize, encrkey, out); //int decrSize = tox_pass_key_decrypt(reinterpret_cast<uint8_t*>(data.data()), encrSize, encrkey, out);
int state = tox_pass_decrypt(reinterpret_cast<uint8_t*>(data.data()), encrSize, int decrSize = tox_pass_decrypt(reinterpret_cast<uint8_t*>(data.data()), encrSize,
reinterpret_cast<uint8_t*>(passwd.data()), passwd.size(), out); reinterpret_cast<uint8_t*>(passwd.data()), passwd.size(), out);
qDebug() << state << encrSize << plainSize;
if (decrSize != plainSize)
{
qWarning() << "decryption failed!";
delete out;
return QByteArray();
}
QByteArray ret = QByteArray::fromRawData(reinterpret_cast<const char*>(out), plainSize); QByteArray ret = QByteArray::fromRawData(reinterpret_cast<const char*>(out), plainSize);
return ret; return ret;

View File

@ -35,7 +35,7 @@ private:
QByteArray encrypt(QByteArray data); QByteArray encrypt(QByteArray data);
QByteArray decrypt(QByteArray data); QByteArray decrypt(QByteArray data);
QList<QString> decryptFile(); bool pullFileContent();
void appendToEncrypted(const QString &sql); void appendToEncrypted(const QString &sql);
u_int8_t *encrkey; u_int8_t *encrkey;

View File

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