-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathCipherDialog.cpp
More file actions
157 lines (133 loc) · 6.1 KB
/
CipherDialog.cpp
File metadata and controls
157 lines (133 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "CipherDialog.h"
#include "ui_CipherDialog.h"
#include "sqlitedb.h"
#include <QPushButton>
#include <QRegularExpressionValidator>
#include <QDesktopServices>
#include <QUrl>
#include <QtCore/qmath.h>
CipherDialog::CipherDialog(QWidget* parent, bool encrypt) :
QDialog(parent),
ui(new Ui::CipherDialog),
encryptMode(encrypt),
rawKeyValidator(new QRegularExpressionValidator(QRegularExpression("\\A(0x[a-fA-F0-9]+)\\Z")))
{
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);
}
// Set the default encryption settings depending on the SQLCipher version we use
QString sqlite_version, sqlcipher_version;
DBBrowserDB::getSqliteVersion(sqlite_version, sqlcipher_version);
if(sqlcipher_version.startsWith('4'))
ui->radioEncryptionSqlCipher4->setChecked(true);
else
ui->radioEncryptionSqlCipher3->setChecked(true);
}
CipherDialog::~CipherDialog()
{
delete rawKeyValidator;
delete ui;
}
CipherSettings CipherDialog::getCipherSettings() const
{
CipherSettings::KeyFormats keyFormat = CipherSettings::getKeyFormat(ui->comboKeyFormat->currentIndex());
std::string password = ui->editPassword->text().toStdString();
int pageSize = ui->comboPageSize->itemData(ui->comboPageSize->currentIndex()).toInt();
CipherSettings cipherSettings;
cipherSettings.setKeyFormat(keyFormat);
cipherSettings.setPassword(password);
cipherSettings.setPageSize(pageSize);
cipherSettings.setKdfIterations(ui->spinKdfIterations->value());
cipherSettings.setHmacAlgorithm("HMAC_" + ui->comboHmacAlgorithm->currentText().toStdString());
cipherSettings.setKdfAlgorithm("PBKDF2_HMAC_" + ui->comboKdfAlgorithm->currentText().toStdString());
cipherSettings.setPlaintextHeaderSize(ui->plaintextHeaderSize->value());
return cipherSettings;
}
void CipherDialog::checkInputFields()
{
CipherSettings::KeyFormats keyFormat = CipherSettings::getKeyFormat(ui->comboKeyFormat->currentIndex());
if(sender() == ui->comboKeyFormat)
{
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) {
const QString password1 = ui->editPassword->text();
valid = password1 == ui->editPassword2->text();
if (keyFormat == CipherSettings::KeyFormats::RawKey) {
valid &= password1.isEmpty() || password1.length() > 2;
}
}
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
}
void CipherDialog::toggleEncryptionSettings()
{
if(ui->radioEncryptionSqlCipher3->isChecked())
{
// SQLCipher3
ui->comboPageSize->setCurrentText(QLocale().toString(1024));
ui->spinKdfIterations->setValue(64000);
ui->comboHmacAlgorithm->setCurrentText("SHA1");
ui->comboKdfAlgorithm->setCurrentText("SHA1");
ui->plaintextHeaderSize->setValue(0);
ui->comboPageSize->setEnabled(false);
ui->spinKdfIterations->setEnabled(false);
ui->comboHmacAlgorithm->setEnabled(false);
ui->comboKdfAlgorithm->setEnabled(false);
ui->plaintextHeaderSize->setEnabled(false);
} else if(ui->radioEncryptionSqlCipher4->isChecked()) {
// SQLCipher4
ui->comboPageSize->setCurrentText(QLocale().toString(4096));
ui->spinKdfIterations->setValue(256000);
ui->comboHmacAlgorithm->setCurrentText("SHA512");
ui->comboKdfAlgorithm->setCurrentText("SHA512");
ui->plaintextHeaderSize->setValue(0);
ui->comboPageSize->setEnabled(false);
ui->spinKdfIterations->setEnabled(false);
ui->comboHmacAlgorithm->setEnabled(false);
ui->comboKdfAlgorithm->setEnabled(false);
ui->plaintextHeaderSize->setEnabled(false);
} else if(ui->radioEncryptionCustom->isChecked()) {
// Custom
ui->comboPageSize->setEnabled(true);
ui->spinKdfIterations->setEnabled(true);
ui->comboHmacAlgorithm->setEnabled(true);
ui->comboKdfAlgorithm->setEnabled(true);
ui->plaintextHeaderSize->setEnabled(true);
}
}
void CipherDialog::help() {
QDesktopServices::openUrl(QUrl("https://github.com/sqlitebrowser/sqlitebrowser/wiki/Encrypted-Databases"));
}