From 71e42cf6c8726a189c6fb0552792267d92a19e55 Mon Sep 17 00:00:00 2001 From: dubslow Date: Sun, 12 Oct 2014 05:24:57 -0500 Subject: [PATCH] add decryption --- src/core.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++------- src/core.h | 2 ++ 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/core.cpp b/src/core.cpp index 70e2f1cf4..29265c27c 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -1071,14 +1071,25 @@ bool Core::loadConfiguration() } else if (error == 1) // Encrypted data save { - qWarning() << "Core: Can not open encrypted tox save"; - if (QMessageBox::Ok != QMessageBox::warning(nullptr, tr("Encrypted profile"), - tr("Your tox profile seems to be encrypted, qTox can't open it\nDo you want to erase this profile ?"), - QMessageBox::Ok | QMessageBox::Cancel)) + if (!pwhash) { - qWarning() << "Core: Couldn't open encrypted save, giving up"; - configurationFile.close(); - return false; + qWarning() << "Core: Can not open encrypted tox save"; + if (QMessageBox::Ok != QMessageBox::warning(nullptr, tr("Encrypted profile"), + tr("Your tox profile seems to be encrypted, qTox can't open it\nDo you want to erase this profile ?"), + QMessageBox::Ok | QMessageBox::Cancel)) + { + qWarning() << "Core: Couldn't open encrypted save, giving up"; + configurationFile.close(); + return false; + } + } + else + { /* + while (error != 0) + { + error = tox_encrypted_load(tox, reinterpret_cast(data.data()), data.size(), pwhash, TOX_HASH_LENGTH); + // something something QInputDialog new password + } */ } } } @@ -1467,6 +1478,20 @@ QByteArray Core::encryptData(const QByteArray& data) return QByteArray(reinterpret_cast(encrypted), data.size() + tox_pass_encryption_extra_length()); } +QByteArray Core::decryptData(const QByteArray& data) +{ + if (!pwhash) + return QByteArray(); + int sz = data.size() - tox_pass_encryption_extra_length(); + uint8_t decrypted[sz]; + if (tox_pass_decrypt(reinterpret_cast(data.data()), data.size(), pwhash, TOX_HASH_LENGTH, decrypted) != sz) + { + qWarning() << "Core::decryptData: decryption failed"; + return QByteArray(); + } + return QByteArray(reinterpret_cast(decrypted), sz); +} + bool Core::encryptFile(const QString& path) { if (!pwhash) @@ -1495,3 +1520,22 @@ bool Core::encryptFile(const QString& path) file.close(); return true; } + +QByteArray Core::decryptFile(const QString& path) +{ + if (!pwhash) + return QByteArray(); + QFile file(path); + if (!file.exists()) { + qWarning() << "Core::decryptFile: file" << path << "DNE"; + return QByteArray(); + } + if (!file.open(QIODevice::ReadOnly)) { + qCritical() << "Core::decryptFile:" << path << " cannot be opened"; + return QByteArray(); + } + QByteArray data = file.readAll(); + file.close(); + + return decryptData(data); +} diff --git a/src/core.h b/src/core.h index ce49a4342..41a95d626 100644 --- a/src/core.h +++ b/src/core.h @@ -98,6 +98,8 @@ public slots: void clearPassword(); bool encryptFile(const QString& path); QByteArray encryptData(const QByteArray& data); + QByteArray decryptData(const QByteArray& data); + QByteArray decryptFile(const QString& path); signals: void connected();