mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
encrypted db uses core encryption
This commit is contained in:
parent
041ed3b998
commit
81003b8523
|
@ -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;
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
#include "encrypteddb.h"
|
||||
#include "src/misc/settings.h"
|
||||
#include "src/core.h"
|
||||
|
||||
#include <tox/toxencryptsave.h>
|
||||
|
||||
|
@ -23,17 +24,9 @@
|
|||
#include <QDebug>
|
||||
#include <QSqlError>
|
||||
|
||||
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<uint8_t*>(key_ba.data()), key_ba.size(), encrkey);
|
||||
passwd = "test";
|
||||
|
||||
qDebug() << QByteArray::fromRawData(reinterpret_cast<char *>(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<QString> 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<uint8_t*>(data.data()), plainSize, encrkey, out);
|
||||
int state = tox_pass_encrypt(reinterpret_cast<uint8_t*>(data.data()), plainSize,
|
||||
reinterpret_cast<uint8_t*>(passwd.data()), passwd.size(), out);
|
||||
qDebug() << state;
|
||||
|
||||
QByteArray ret = QByteArray::fromRawData(reinterpret_cast<const char*>(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<uint8_t*>(data.data()), encrSize, encrkey, out);
|
||||
int state = tox_pass_decrypt(reinterpret_cast<uint8_t*>(data.data()), encrSize,
|
||||
reinterpret_cast<uint8_t*>(passwd.data()), passwd.size(), out);
|
||||
qDebug() << state << encrSize << plainSize;
|
||||
|
||||
QByteArray ret = QByteArray::fromRawData(reinterpret_cast<const char*>(out), plainSize);
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -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<QString> decryptFile();
|
||||
void appendToEncrypted(const QString &sql);
|
||||
|
||||
u_int8_t *encrkey;
|
||||
QFile encrFile;
|
||||
QByteArray passwd;
|
||||
|
||||
qint64 plainChunkSize;
|
||||
qint64 encryptedChunkSize;
|
||||
|
|
Loading…
Reference in New Issue
Block a user