1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-11 23:37:59 +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 "preferences.h"
#include <QCryptographicHash>
#include <QDir> #include <QDir>
#include <QLocale> #include <QLocale>
#include <QMutableListIterator> #include <QMutableListIterator>
@ -725,22 +724,14 @@ void Preferences::setDynDNSPassword(const QString &password)
} }
// Advanced settings // 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(); setValue("Locking/password_PBKDF2", password);
}
void Preferences::setUILockPassword(const QString &clearPassword)
{
QCryptographicHash md5(QCryptographicHash::Md5);
md5.addData(clearPassword.toLocal8Bit());
QString md5Password = md5.result().toHex();
setValue("Locking/password", md5Password);
} }
bool Preferences::isUILocked() const bool Preferences::isUILocked() const

View File

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

View File

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

View File

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