Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ set(SQLB_MOC_HDR
src/FindReplaceDialog.h
src/ExtendedScintilla.h
src/FileExtensionManager.h
src/CipherSettings.h
src/DotenvFormat.h
)

set(SQLB_SRC
Expand Down Expand Up @@ -182,6 +184,8 @@ set(SQLB_SRC
src/ExtendedScintilla.cpp
src/FileExtensionManager.cpp
src/Data.cpp
src/CipherSettings.cpp
src/DotenvFormat.cpp
)

set(SQLB_FORMS
Expand Down
29 changes: 14 additions & 15 deletions src/CipherDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,33 @@ CipherDialog::~CipherDialog()
delete ui;
}

CipherDialog::KeyFormats CipherDialog::keyFormat() const
CipherSettings CipherDialog::getCipherSettings() const
{
return static_cast<CipherDialog::KeyFormats>(ui->comboKeyFormat->currentIndex());
}
CipherSettings::KeyFormats keyFormat = CipherSettings::getKeyFormat(ui->comboKeyFormat->currentIndex());
QString password = ui->editPassword->text();
int pageSize = ui->comboPageSize->itemData(ui->comboPageSize->currentIndex()).toInt();

QString CipherDialog::password() const
{
if(keyFormat() == KeyFormats::Passphrase)
return QString("'%1'").arg(ui->editPassword->text().replace("'", "''"));
else
return QString("\"x'%1'\"").arg(ui->editPassword->text().mid(2)); // Remove the '0x' part at the beginning
}
CipherSettings cipherSettings;

int CipherDialog::pageSize() const
{
return ui->comboPageSize->itemData(ui->comboPageSize->currentIndex()).toInt();
cipherSettings.setKeyFormat(keyFormat);
cipherSettings.setPassword(password);
cipherSettings.setPageSize(pageSize);

return cipherSettings;
}

void CipherDialog::checkInputFields()
{
if(sender() == ui->comboKeyFormat)
{
if(keyFormat() == KeyFormats::Passphrase)
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() == KeyFormats::RawKey) {
} else if(keyFormat == CipherSettings::KeyFormats::RawKey) {
ui->editPassword->setValidator(rawKeyValidator);
ui->editPassword2->setValidator(rawKeyValidator);
ui->editPassword->setPlaceholderText("0x...");
Expand Down
13 changes: 3 additions & 10 deletions src/CipherDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <QDialog>

#include "CipherSettings.h"

class QRegExpValidator;

namespace Ui {
Expand All @@ -14,21 +16,12 @@ class CipherDialog : public QDialog
Q_OBJECT

public:
enum KeyFormats
{
Passphrase,
RawKey
};

// Set the encrypt parameter to true when the dialog is used to encrypt a database;
// set it to false if the dialog is used to ask the user for the key to decrypt a file.
explicit CipherDialog(QWidget* parent, bool encrypt);
~CipherDialog();

// Allow read access to the input fields
KeyFormats keyFormat() const;
QString password() const;
int pageSize() const;
CipherSettings getCipherSettings() const;

private:
Ui::CipherDialog* ui;
Expand Down
49 changes: 49 additions & 0 deletions src/CipherSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "CipherSettings.h"

CipherSettings::KeyFormats CipherSettings::getKeyFormat() const
{
return keyFormat;
}

void CipherSettings::setKeyFormat(const KeyFormats &value)
{
keyFormat = value;
}

QString CipherSettings::getPassword() const
{
if(keyFormat == Passphrase)
{
QString tempPassword = password;

tempPassword.replace("'", "''");

return QString("'%1'").arg(tempPassword);
} else {
// Remove the '0x' part at the beginning
return QString("\"x'%1'\"").arg(password.mid(2));
}
}

void CipherSettings::setPassword(const QString &value)
{
password = value;
}

int CipherSettings::getPageSize() const
{
if (pageSize == 0)
return defaultPageSize;

return pageSize;
}

void CipherSettings::setPageSize(int value)
{
pageSize = value;
}

CipherSettings::KeyFormats CipherSettings::getKeyFormat(int rawKeyFormat)
{
return static_cast<CipherSettings::KeyFormats>(rawKeyFormat);
}
34 changes: 34 additions & 0 deletions src/CipherSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef CIPHERSETTINGS_H
#define CIPHERSETTINGS_H

#include <QString>

class CipherSettings
{
public:
enum KeyFormats
{
Passphrase,
RawKey
};

static const int defaultPageSize = 1024;

KeyFormats getKeyFormat() const;
void setKeyFormat(const KeyFormats &value);

QString getPassword() const;
void setPassword(const QString &value);

int getPageSize() const;
void setPageSize(int value);

static KeyFormats getKeyFormat(int rawKeyFormat);

private:
KeyFormats keyFormat;
QString password;
int pageSize;
};

#endif // CIPHERSETTINGS_H
28 changes: 28 additions & 0 deletions src/DotenvFormat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "DotenvFormat.h"

#include <QRegularExpression>
#include <QTextStream>

bool DotenvFormat::readEnvFile(QIODevice &device, QSettings::SettingsMap &map)
{
QTextStream in(&device);

QString line;

QRegularExpression keyValueRegex("^\\s*([\\w\\.\\-]+)\\s*=\\s*(.*)\\s*$");

while (in.readLineInto(&line)) {
QRegularExpressionMatch match = keyValueRegex.match(line);

if (match.capturedLength() < 3) {
continue;
}

QString key = match.captured(1);
QString value = match.captured(2);

map.insert(key, value);
}

return true;
}
13 changes: 13 additions & 0 deletions src/DotenvFormat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef DOTENVFORMAT_H
#define DOTENVFORMAT_H

#include <QIODevice>
#include <QSettings>

class DotenvFormat
{
public:
static bool readEnvFile(QIODevice &device, QSettings::SettingsMap &map);
};

#endif // DOTENVFORMAT_H
10 changes: 6 additions & 4 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2513,8 +2513,8 @@ void MainWindow::updateFilter(int column, const QString& value)
void MainWindow::editEncryption()
{
#ifdef ENABLE_SQLCIPHER
CipherDialog dialog(this, true);
if(dialog.exec())
CipherDialog cipherDialog(this, true);
if(cipherDialog.exec())
{
// Show progress dialog even though we can't provide any detailed progress information but this
// process might take some time.
Expand All @@ -2536,14 +2536,16 @@ void MainWindow::editEncryption()
file.close();
}

CipherSettings cipherSettings = cipherDialog.getCipherSettings();

// Attach a new database using the new settings
qApp->processEvents();
if(ok)
ok = db.executeSQL(QString("ATTACH DATABASE '%1' AS sqlitebrowser_edit_encryption KEY %2;").arg(db.currentFile() + ".enctemp").arg(dialog.password()),
ok = db.executeSQL(QString("ATTACH DATABASE '%1' AS sqlitebrowser_edit_encryption KEY %2;").arg(db.currentFile() + ".enctemp").arg(cipherSettings.getPassword()),
false, false);
qApp->processEvents();
if(ok)
ok = db.executeSQL(QString("PRAGMA sqlitebrowser_edit_encryption.cipher_page_size = %1").arg(dialog.pageSize()), false, false);
ok = db.executeSQL(QString("PRAGMA sqlitebrowser_edit_encryption.cipher_page_size = %1").arg(cipherSettings.getPageSize()), false, false);

// Export the current database to the new one
qApp->processEvents();
Expand Down
2 changes: 1 addition & 1 deletion src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#define SETTINGS_H

#include <QApplication>
#include <QVariant>
#include <QHash>
#include <QVariant>

class Settings
{
Expand Down
Loading