Files
sqlitebrowser/src/CipherDialog.cpp
Iulian Onofrei 3cdc65a63f Add automatic crypted databases open via dotenvs (#1404)
* Rename confusing variables

* Fix some project warnings

* Fix code style

* Add constant for the default page size

* Move KeyFormats enum to CipherSettings

* Fix code style

* Fix memory leak

* Stop relying on CipherDialog for encryption settings management

* Fix code style

* Add .env format for QSettings

* Add automatic crypted databases open via dotenvs

This adds support for `.env` files next to the crypted databases that
are to be opened that contains the needed cipher settings.

The only required one is the plain-text password as a value for the key
with the name of the database like this:

    myCryptedDatabase.sqlite = MyPassword

This way, databases with a different extension are supported too:

    myCryptedDatabase.db = MyPassword

You can also specify a custom page size adding a different line
(anywhere in the file) like this:

    myCryptedDatabase.db_pageSize = 2048

If not specified, `1024` is used.

You can also specify the format of the specified key using the
associated integer id:

    anotherCryptedDatabase.sqlite = 0xCAFEBABE
    anotherCryptedDatabase.sqlite_keyFormat = 1

where `1` means a Raw key. If not specified, `0` is used, which means a
simple text Passphrase.

Dotenv files (`.env`) are already used on other platforms and by
different tools to manage environment variables, and it's recommended
to be ignored from version control systems, so they won't leak.

* Add new files to CMakeLists

* Move DotenvFormat include to the implementation

* Fix build error

* Remove superfluous method

(related to ac51c23)

* Remove superfluous checks

* Fix memory leaks

(introduced by 94bbb46)

* Fix code style

* Make dotenv related variable and comment clearer

* Remove duplicated code

* Remove unused forward declaration

(introduced by e5a0293)
2018-07-10 20:46:17 +02:00

94 lines
3.4 KiB
C++

#include "CipherDialog.h"
#include "ui_CipherDialog.h"
#include <QPushButton>
#include <QRegExpValidator>
#include <QtCore/qmath.h>
CipherDialog::CipherDialog(QWidget* parent, bool encrypt) :
QDialog(parent),
ui(new Ui::CipherDialog),
encryptMode(encrypt),
rawKeyValidator(new QRegExpValidator(QRegExp("0x[a-fA-F0-9]+")))
{
ui->setupUi(this);
int minimumPageSizeExponent = 9;
int maximumPageSizeExponent = 16;
int defaultPageSizeExponent = 10;
for(int exponent = minimumPageSizeExponent; exponent <= maximumPageSizeExponent; exponent++)
{
int pageSize = static_cast<int>(qPow(2, exponent));
ui->comboPageSize->addItem(QLocale().toString(pageSize), pageSize);
if (exponent == defaultPageSizeExponent)
ui->comboPageSize->setCurrentIndex(exponent - minimumPageSizeExponent);
}
ui->comboPageSize->setMinimumWidth(ui->editPassword->width());
if(encrypt)
{
ui->labelDialogDescription->setText(tr("Please set a key to encrypt the database.\nNote that if you change any of the other, optional, settings you'll need "
"to re-enter them as well every time you open the database file.\nLeave the password fields empty to disable the "
"encryption.\nThe encryption process might take some time and you should have a backup copy of your database! Unsaved "
"changes are applied before modifying the encryption."));
} else {
ui->labelDialogDescription->setText(tr("Please enter the key used to encrypt the database.\nIf any of the other settings were altered for this database file "
"you need to provide this information as well."));
ui->editPassword2->setVisible(false);
ui->labelPassword2->setVisible(false);
}
}
CipherDialog::~CipherDialog()
{
delete rawKeyValidator;
delete ui;
}
CipherSettings CipherDialog::getCipherSettings() const
{
CipherSettings::KeyFormats keyFormat = CipherSettings::getKeyFormat(ui->comboKeyFormat->currentIndex());
QString password = ui->editPassword->text();
int pageSize = ui->comboPageSize->itemData(ui->comboPageSize->currentIndex()).toInt();
CipherSettings cipherSettings;
cipherSettings.setKeyFormat(keyFormat);
cipherSettings.setPassword(password);
cipherSettings.setPageSize(pageSize);
return cipherSettings;
}
void CipherDialog::checkInputFields()
{
if(sender() == ui->comboKeyFormat)
{
CipherSettings::KeyFormats keyFormat = CipherSettings::getKeyFormat(ui->comboKeyFormat->currentIndex());
if(keyFormat == CipherSettings::KeyFormats::Passphrase)
{
ui->editPassword->setValidator(nullptr);
ui->editPassword2->setValidator(nullptr);
ui->editPassword->setPlaceholderText("");
} else if(keyFormat == CipherSettings::KeyFormats::RawKey) {
ui->editPassword->setValidator(rawKeyValidator);
ui->editPassword2->setValidator(rawKeyValidator);
ui->editPassword->setPlaceholderText("0x...");
}
ui->editPassword->setText("");
ui->editPassword2->setText("");
}
bool valid = true;
if(encryptMode)
valid = ui->editPassword->text() == ui->editPassword2->text();
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
}