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

encrypted history: class constructor: refacroring

This commit is contained in:
apprb 2014-11-08 23:28:19 +09:00 committed by Dubslow
parent a9fb75b5ed
commit a432a16e01
3 changed files with 35 additions and 19 deletions

View File

@ -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<const uint8_t*>(data.data()), data.size(), pwsaltedkeys[passtype], decrypted) != sz)
int decr_size = tox_pass_key_decrypt(reinterpret_cast<const uint8_t*>(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<const char*>(salt), tox_pass_salt_length());
QByteArray res = QByteArray::fromRawData(reinterpret_cast<const char*>(salt), tox_pass_salt_length());
qDebug() << res.toBase64();
return res;
}

View File

@ -28,24 +28,32 @@ qint64 EncryptedDb::plainChunkSize = 4096;
qint64 EncryptedDb::encryptedChunkSize = EncryptedDb::plainChunkSize + tox_pass_encryption_extra_length();
EncryptedDb::EncryptedDb(const QString &fname, QList<QString> 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;
}

View File

@ -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;