From 9df2b49d07bc459afdb69f2c2958ac1aa05392eb Mon Sep 17 00:00:00 2001 From: dubslow Date: Sat, 11 Oct 2014 18:58:10 -0500 Subject: [PATCH] more --- core.cpp | 41 ++++++++++++++++++++++++++++++++++------- core.h | 1 + widget/widget.cpp | 2 +- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/core.cpp b/core.cpp index 561c2965c..dd81e575e 100644 --- a/core.cpp +++ b/core.cpp @@ -1415,15 +1415,46 @@ QList Core::splitMessage(const QString &message) void Core::setPassword(QString& password) { + if (password.isEmpty()) + { + clearPassword(); + return; + } if (!pwhash) pwhash = new uint8_t[TOX_HASH_LENGTH]; + CString str(password); tox_hash(pwhash, str.data(), str.size()); password.clear(); } +void Core::clearPassword() +{ + if (pwhash) + { + delete[] pwhash; + pwhash = nullptr; + } + +} + +QByteArray Core::encryptData(const QByteArray& data) +{ + if (!pwhash) + return; + uint8_t encrypted[data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH]; + if (tox_pass_encrypt(data.data(), data.size(), pwhash, TOX_HASH_LENGTH, encrypted) == -1) + { + qWarning() << "Core::encryptData: encryption failed"; + return QByteArray(); + } + return QByteArray(reinterpret_castencrypted, data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH]); +} + bool Core::encryptFile(const QString& path) { + if (!pwhash) + return false; QFile file(path); if (!file.exists()) { qWarning() << "Core::encryptFile: file" << path << "DNE"; @@ -1436,19 +1467,15 @@ bool Core::encryptFile(const QString& path) QByteArray data = file.readAll(); file.close(); - uint8_t[] out = new uint8_t[data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH]; - if (tox_pass_encrypt(data.data(), data.size(), pwhash, TOX_HASH_LENGTH, out) - == -1) - { - qWarning() << "Core::encryptFile:" << "encryption of" << path << "failed"; + data = encryptData(data); + if (data.isEmpty()) return false; - } if (!file.open(QIODevice::WriteOnly)) { qCritical() << "Core::encryptFile:" << path << " cannot be opened for writing"; return false; } - file.write(reinterpret_castout, data.size() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH); + file.write(data.data(), data.size()); file.close(); return true; } diff --git a/core.h b/core.h index 33cad177a..ef16cc645 100644 --- a/core.h +++ b/core.h @@ -96,6 +96,7 @@ public slots: void setPassword(QString& password); bool encryptFile(const QString& path); + QByteArray encryptData(const QByteArray& data); signals: void connected(); diff --git a/widget/widget.cpp b/widget/widget.cpp index 20efebaec..ade73e06e 100644 --- a/widget/widget.cpp +++ b/widget/widget.cpp @@ -219,7 +219,7 @@ QString Widget::getUsername() void Widget::onAvatarClicked() { QString filename = QFileDialog::getOpenFileName(this, tr("Choose a profile picture"), QDir::homePath()); - if (filename == "") + if (filename.isEmpty()) return; QFile file(filename); file.open(QIODevice::ReadOnly);