cipher: Fix build option and use a dialog for entering the password · gerram/sqlitebrowser@167d8cb · GitHub
Skip to content

Commit 167d8cb

Browse files
committed
cipher: Fix build option and use a dialog for entering the password
Fix the sqlcipher build option in the qmake project to actually enable the SQLCipher support. Add a new dialog called CipherDialog which is used to ask the user for the password and other encryption details instead of using standard Qt input dialogs.
1 parent 9ba36d0 commit 167d8cb

8 files changed

Lines changed: 265 additions & 21 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 5 additions & 2 deletions

‎src/AboutDialog.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ AboutDialog::AboutDialog(QWidget *parent) :
1212

1313
ui->label_version->setText(ui->label_version->text() + " " + APP_VERSION);
1414
ui->label_versionqt->setText(ui->label_versionqt->text() + " " + QT_VERSION_STR);
15-
#ifdef SQLCIPHER
15+
#ifdef ENABLE_SQLCIPHER
1616
ui->label_versionsqlite->setText(ui->label_versionsqlite->text().replace("SQLite", "SQLCipher") + " " + SQLITE_VERSION);
1717
#else
1818
ui->label_versionsqlite->setText(ui->label_versionsqlite->text() + " " + SQLITE_VERSION);

‎src/CipherDialog.cpp‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "CipherDialog.h"
2+
#include "ui_CipherDialog.h"
3+
4+
#include <QPushButton>
5+
6+
CipherDialog::CipherDialog(QWidget* parent, bool encrypt) :
7+
QDialog(parent),
8+
ui(new Ui::CipherDialog),
9+
encryptMode(encrypt)
10+
{
11+
ui->setupUi(this);
12+
13+
if(encrypt)
14+
{
15+
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 "
16+
"to re-enter them as well every time you open the database file."));
17+
} else {
18+
ui->labelDialogDescription->setText(tr("Please enter the key used to encrypt the database.\nIf any of the other setting were altered for this database file "
19+
"you need to provide this information as well."));
20+
ui->editPassword2->setVisible(false);
21+
ui->labelPassword2->setVisible(false);
22+
}
23+
}
24+
25+
CipherDialog::~CipherDialog()
26+
{
27+
delete ui;
28+
}
29+
30+
QString CipherDialog::password()
31+
{
32+
return ui->editPassword->text();
33+
}
34+
35+
int CipherDialog::pageSize()
36+
{
37+
return ui->spinPageSize->value();
38+
}
39+
40+
void CipherDialog::checkInputFields()
41+
{
42+
bool valid = true;
43+
if(encryptMode)
44+
valid = ui->editPassword->text() == ui->editPassword2->text();
45+
46+
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
47+
}

‎src/CipherDialog.h‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef CIPHERDIALOG_H
2+
#define CIPHERDIALOG_H
3+
4+
#include <QDialog>
5+
6+
namespace Ui {
7+
class CipherDialog;
8+
}
9+
10+
class CipherDialog : public QDialog
11+
{
12+
Q_OBJECT
13+
14+
public:
15+
// Set the encrypt parameter to true when the dialog is used to encrypt a database;
16+
// set it to false if the dialog is used to ask the user for the key to decrypt a file.
17+
explicit CipherDialog(QWidget* parent, bool encrypt);
18+
~CipherDialog();
19+
20+
// Allow read access tothe input fields
21+
QString password();
22+
int pageSize();
23+
24+
private:
25+
Ui::CipherDialog* ui;
26+
bool encryptMode;
27+
28+
private slots:
29+
void checkInputFields();
30+
};
31+
32+
#endif

‎src/CipherDialog.ui‎

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>CipherDialog</class>
4+
<widget class="QDialog" name="CipherDialog">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>475</width>
10+
<height>147</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>SQLCipher encryption</string>
15+
</property>
16+
<layout class="QVBoxLayout" name="verticalLayout">
17+
<item>
18+
<widget class="QLabel" name="labelDialogDescription"/>
19+
</item>
20+
<item>
21+
<layout class="QFormLayout" name="formLayout">
22+
<property name="fieldGrowthPolicy">
23+
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
24+
</property>
25+
<item row="0" column="0">
26+
<widget class="QLabel" name="labelPassword">
27+
<property name="text">
28+
<string>&amp;Password</string>
29+
</property>
30+
<property name="buddy">
31+
<cstring>editPassword</cstring>
32+
</property>
33+
</widget>
34+
</item>
35+
<item row="0" column="1">
36+
<widget class="QLineEdit" name="editPassword">
37+
<property name="echoMode">
38+
<enum>QLineEdit::Password</enum>
39+
</property>
40+
</widget>
41+
</item>
42+
<item row="1" column="0">
43+
<widget class="QLabel" name="labelPassword2">
44+
<property name="text">
45+
<string>&amp;Reenter password</string>
46+
</property>
47+
<property name="buddy">
48+
<cstring>editPassword2</cstring>
49+
</property>
50+
</widget>
51+
</item>
52+
<item row="1" column="1">
53+
<widget class="QLineEdit" name="editPassword2">
54+
<property name="echoMode">
55+
<enum>QLineEdit::Password</enum>
56+
</property>
57+
</widget>
58+
</item>
59+
<item row="2" column="0">
60+
<widget class="QLabel" name="label_3">
61+
<property name="text">
62+
<string>Page &amp;size</string>
63+
</property>
64+
<property name="buddy">
65+
<cstring>spinPageSize</cstring>
66+
</property>
67+
</widget>
68+
</item>
69+
<item row="2" column="1">
70+
<widget class="QSpinBox" name="spinPageSize">
71+
<property name="minimum">
72+
<number>1</number>
73+
</property>
74+
<property name="maximum">
75+
<number>8092</number>
76+
</property>
77+
<property name="value">
78+
<number>4096</number>
79+
</property>
80+
</widget>
81+
</item>
82+
</layout>
83+
</item>
84+
<item>
85+
<widget class="QDialogButtonBox" name="buttonBox">
86+
<property name="orientation">
87+
<enum>Qt::Horizontal</enum>
88+
</property>
89+
<property name="standardButtons">
90+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
91+
</property>
92+
</widget>
93+
</item>
94+
</layout>
95+
</widget>
96+
<resources/>
97+
<connections>
98+
<connection>
99+
<sender>buttonBox</sender>
100+
<signal>accepted()</signal>
101+
<receiver>CipherDialog</receiver>
102+
<slot>accept()</slot>
103+
<hints>
104+
<hint type="sourcelabel">
105+
<x>227</x>
106+
<y>110</y>
107+
</hint>
108+
<hint type="destinationlabel">
109+
<x>157</x>
110+
<y>126</y>
111+
</hint>
112+
</hints>
113+
</connection>
114+
<connection>
115+
<sender>buttonBox</sender>
116+
<signal>rejected()</signal>
117+
<receiver>CipherDialog</receiver>
118+
<slot>reject()</slot>
119+
<hints>
120+
<hint type="sourcelabel">
121+
<x>295</x>
122+
<y>116</y>
123+
</hint>
124+
<hint type="destinationlabel">
125+
<x>286</x>
126+
<y>126</y>
127+
</hint>
128+
</hints>
129+
</connection>
130+
<connection>
131+
<sender>editPassword</sender>
132+
<signal>textChanged(QString)</signal>
133+
<receiver>CipherDialog</receiver>
134+
<slot>checkInputFields()</slot>
135+
<hints>
136+
<hint type="sourcelabel">
137+
<x>150</x>
138+
<y>38</y>
139+
</hint>
140+
<hint type="destinationlabel">
141+
<x>155</x>
142+
<y>26</y>
143+
</hint>
144+
</hints>
145+
</connection>
146+
<connection>
147+
<sender>editPassword2</sender>
148+
<signal>textChanged(QString)</signal>
149+
<receiver>CipherDialog</receiver>
150+
<slot>checkInputFields()</slot>
151+
<hints>
152+
<hint type="sourcelabel">
153+
<x>201</x>
154+
<y>57</y>
155+
</hint>
156+
<hint type="destinationlabel">
157+
<x>206</x>
158+
<y>46</y>
159+
</hint>
160+
</hints>
161+
</connection>
162+
</connections>
163+
<slots>
164+
<slot>checkInputFields()</slot>
165+
</slots>
166+
</ui>

‎src/sqlite.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef SQLITE_H
22
#define SQLITE_H
33

4-
#ifdef SQLCIPHER
4+
#ifdef ENABLE_SQLCIPHER
55
#define SQLITE_TEMP_STORE 2
66
#define SQLITE_HAS_CODEC
77
#include <sqlcipher/sqlite3.h>

‎src/sqlitedb.cpp‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "sqlitedb.h"
22
#include "sqlitetablemodel.h"
33
#include "sqlite.h"
4+
#include "CipherDialog.h"
45

56
#include <QFile>
67
#include <QMessageBox>
@@ -71,24 +72,16 @@ bool DBBrowserDB::open(const QString& db)
7172
err = sqlite3_prepare_v2(_db, utf8Statement, utf8Statement.length(), &vm, &tail);
7273
if(sqlite3_step(vm) != SQLITE_ROW)
7374
{
74-
#ifdef SQLCIPHER
75-
QString pass = QInputDialog::getText(0, qApp->applicationName(),
76-
QObject::tr("Couldn't read from database file. This means it is either not a valid SQLite3 "
77-
"database or it is encrypted.\nIn the latter case you can provide a passphrase to open the file. Note"
78-
"that only databases encrypted using SQLCipher are supported."));
79-
if(pass.isEmpty())
75+
#ifdef ENABLE_SQLCIPHER
76+
CipherDialog cipher(0, false);
77+
if(cipher.exec())
8078
{
79+
sqlite3_key(_db, cipher.password().toUtf8(), cipher.password().toUtf8().length());
80+
sqlite3_exec(_db, QString("PRAGMA cipher_page_size = %1;").arg(cipher.pageSize()).toUtf8(), NULL, NULL, NULL);
81+
} else {
8182
sqlite3_close(_db);
8283
_db = 0;
8384
return false;
84-
} else {
85-
int pagesize = QInputDialog::getInt(0, qApp->applicationName(),
86-
QObject::tr("If the database has been encrypted using a different page size than normally (i.e. 1024 bytes) this "
87-
"value is required, too."),
88-
1024, 0);
89-
if(pagesize == 0) pagesize = 1024;
90-
sqlite3_key(_db, pass.toUtf8(), pass.toUtf8().length());
91-
sqlite3_exec(_db, QString("PRAGMA cipher_page_size = %1;").arg(pagesize).toUtf8(), NULL, NULL, NULL);
9285
}
9386
#else
9487
sqlite3_close(_db);

‎src/src.pro‎

Lines changed: 6 additions & 3 deletions

0 commit comments

Comments
 (0)