1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-11 15:27:54 +00:00

Apply PBKDF2 to GUI lock

This commit is contained in:
Chocobo1 2018-11-21 21:40:31 +08:00
parent 05d6a29416
commit 2c8890bd06
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
4 changed files with 40 additions and 57 deletions

View File

@ -29,7 +29,6 @@
#include "preferences.h"
#include <QCryptographicHash>
#include <QDir>
#include <QLocale>
#include <QMutableListIterator>
@ -725,22 +724,14 @@ void Preferences::setDynDNSPassword(const QString &password)
}
// Advanced settings
void Preferences::clearUILockPassword()
QByteArray Preferences::getUILockPassword() const
{
setValue("Locking/password", QString());
return value("Locking/password_PBKDF2").toByteArray();
}
QString Preferences::getUILockPasswordMD5() const
void Preferences::setUILockPassword(const QByteArray &password)
{
return value("Locking/password").toString();
}
void Preferences::setUILockPassword(const QString &clearPassword)
{
QCryptographicHash md5(QCryptographicHash::Md5);
md5.addData(clearPassword.toLocal8Bit());
QString md5Password = md5.result().toHex();
setValue("Locking/password", md5Password);
setValue("Locking/password_PBKDF2", password);
}
bool Preferences::isUILocked() const

View File

@ -229,9 +229,8 @@ public:
void setDynDNSPassword(const QString &password);
// Advanced settings
void setUILockPassword(const QString &clearPassword);
void clearUILockPassword();
QString getUILockPasswordMD5() const;
QByteArray getUILockPassword() const;
void setUILockPassword(const QByteArray &password);
bool isUILocked() const;
void setUILocked(bool locked);
bool isAutoRunEnabled() const;

View File

@ -30,7 +30,6 @@
#include <QClipboard>
#include <QCloseEvent>
#include <QCryptographicHash>
#include <QDebug>
#include <QDesktopServices>
#include <QFileDialog>
@ -69,6 +68,7 @@
#include "base/utils/foreignapps.h"
#include "base/utils/fs.h"
#include "base/utils/misc.h"
#include "base/utils/password.h"
#include "aboutdialog.h"
#include "addnewtorrentdialog.h"
#include "application.h"
@ -631,45 +631,41 @@ void MainWindow::toolbarFollowSystem()
Preferences::instance()->setToolbarTextPosition(Qt::ToolButtonFollowStyle);
}
void MainWindow::defineUILockPassword()
bool MainWindow::defineUILockPassword()
{
QString oldPassMd5 = Preferences::instance()->getUILockPasswordMD5();
if (oldPassMd5.isNull())
oldPassMd5 = "";
bool ok = false;
QString newClearPassword = AutoExpandableDialog::getText(this, tr("UI lock password"), tr("Please type the UI lock password:"), QLineEdit::Password, oldPassMd5, &ok);
if (ok) {
newClearPassword = newClearPassword.trimmed();
if (newClearPassword.size() < 3) {
QMessageBox::warning(this, tr("Invalid password"), tr("The password should contain at least 3 characters"));
}
else {
if (newClearPassword != oldPassMd5)
Preferences::instance()->setUILockPassword(newClearPassword);
QMessageBox::information(this, tr("Password update"), tr("The UI lock password has been successfully updated"));
}
const QString newPassword = AutoExpandableDialog::getText(this, tr("UI lock password")
, tr("Please type the UI lock password:"), QLineEdit::Password, {}, &ok);
if (!ok)
return false;
if (newPassword.size() < 3) {
QMessageBox::warning(this, tr("Invalid password"), tr("The password should contain at least 3 characters"));
return false;
}
Preferences::instance()->setUILockPassword(Utils::Password::PBKDF2::generate(newPassword));
return true;
}
void MainWindow::clearUILockPassword()
{
QMessageBox::StandardButton answer = QMessageBox::question(this, tr("Clear the password"), tr("Are you sure you want to clear the password?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
const QMessageBox::StandardButton answer = QMessageBox::question(this, tr("Clear the password")
, tr("Are you sure you want to clear the password?"), (QMessageBox::Yes | QMessageBox::No), QMessageBox::No);
if (answer == QMessageBox::Yes)
Preferences::instance()->clearUILockPassword();
Preferences::instance()->setUILockPassword({});
}
void MainWindow::on_actionLock_triggered()
{
Preferences *const pref = Preferences::instance();
// Check if there is a password
if (pref->getUILockPasswordMD5().isEmpty()) {
// Ask for a password
bool ok = false;
QString clearPassword = AutoExpandableDialog::getText(this, tr("UI lock password"), tr("Please type the UI lock password:"), QLineEdit::Password, "", &ok);
if (!ok) return;
pref->setUILockPassword(clearPassword);
if (pref->getUILockPassword().isEmpty()) {
if (!defineUILockPassword())
return;
}
// Lock the interface
m_uiLocked = true;
pref->setUILocked(true);
@ -1049,27 +1045,24 @@ bool MainWindow::unlockUI()
{
if (m_unlockDlgShowing)
return false;
else
m_unlockDlgShowing = true;
bool ok = false;
QString clearPassword = AutoExpandableDialog::getText(this, tr("UI lock password"), tr("Please type the UI lock password:"), QLineEdit::Password, "", &ok);
m_unlockDlgShowing = false;
const QString password = AutoExpandableDialog::getText(this, tr("UI lock password")
, tr("Please type the UI lock password:"), QLineEdit::Password, {}, &ok);
if (!ok) return false;
Preferences *const pref = Preferences::instance();
QString realPassMd5 = pref->getUILockPasswordMD5();
QCryptographicHash md5(QCryptographicHash::Md5);
md5.addData(clearPassword.toLocal8Bit());
QString passwordMd5 = md5.result().toHex();
if (realPassMd5 == passwordMd5) {
m_uiLocked = false;
pref->setUILocked(false);
m_trayIconMenu->setEnabled(true);
return true;
const QByteArray secret = pref->getUILockPassword();
if (!Utils::Password::PBKDF2::verify(secret, password)) {
QMessageBox::warning(this, tr("Invalid password"), tr("The password is invalid"));
return false;
}
QMessageBox::warning(this, tr("Invalid password"), tr("The password is invalid"));
return false;
m_uiLocked = false;
pref->setUILocked(false);
m_trayIconMenu->setEnabled(true);
return true;
}
void MainWindow::notifyOfUpdate(QString)

View File

@ -109,7 +109,7 @@ private slots:
void fullDiskError(BitTorrent::TorrentHandle *const torrent, QString msg) const;
void handleDownloadFromUrlFailure(QString, QString) const;
void tabChanged(int newTab);
void defineUILockPassword();
bool defineUILockPassword();
void clearUILockPassword();
bool unlockUI();
void notifyOfUpdate(QString);