retry on weak password, Vault::create

This commit is contained in:
2026-04-25 13:12:46 +02:00
parent 47f1f7dd74
commit 85541e1db4
3 changed files with 35 additions and 26 deletions

View File

@@ -1,6 +1,5 @@
// TODO: actual fs // TODO: actual fs
#include "mainwindow.h" #include "mainwindow.h"
#include "crypto.h"
#include <QDesktopServices> #include <QDesktopServices>
#include <QDropEvent> #include <QDropEvent>
#include <QFileDialog> #include <QFileDialog>
@@ -39,39 +38,28 @@ MainWindow::MainWindow(QWidget *parent)
path += ".dull"; path += ".dull";
} }
QString password = QInputDialog::getText( QString password;
while (true) {
password = QInputDialog::getText(
this, "Choose a password", "Choose a password", QLineEdit::Password); this, "Choose a password", "Choose a password", QLineEdit::Password);
if (password.length() < 8) { if (password.length() < 8) {
QMessageBox::critical(this, "Error", QMessageBox::critical(this, "Error",
"Password must be at least 8 characters long."); "Password must be at least 8 characters long.");
return; } else {
break;
}
} }
ui->statusbar->showMessage("Creating the vault..."); ui->statusbar->showMessage("Creating the vault...");
QCoreApplication::processEvents(); QCoreApplication::processEvents();
static Botan::AutoSeeded_RNG rng; Vault::create(path.toStdString(), password.toStdString());
auto salt = rng.random_array<16>();
auto key = Crypto::derive_key(password.toStdString(), salt);
const std::string_view content = "LETSGO";
Botan::secure_vector<u8> content_sv(content.begin(), content.end());
auto check_ciphertext = Crypto::encrypt(key, content_sv);
std::ofstream create(path.toStdString(), std::ios::binary);
create.write("DULL", 4);
create.write(to_char_ptr(&VERSION), sizeof(VERSION));
create.write(to_char_ptr(salt.data()), 16);
create.write(to_char_ptr(check_ciphertext.data()), 46);
create.close();
m_vault = m_vault =
std::make_unique<Vault>(path.toStdString(), password.toStdString()); std::make_unique<Vault>(path.toStdString(), password.toStdString());
reload_fs_tree(); ui->statusbar->showMessage("Opened " + path);
ui->statusbar->clearMessage(); reload_fs_tree();
}); });
connect(ui->actionOpen, &QAction::triggered, this, [this]() { connect(ui->actionOpen, &QAction::triggered, this, [this]() {
@@ -95,7 +83,7 @@ MainWindow::MainWindow(QWidget *parent)
m_vault = m_vault =
std::make_unique<Vault>(path.toStdString(), password.toStdString()); std::make_unique<Vault>(path.toStdString(), password.toStdString());
reload_fs_tree(); reload_fs_tree();
ui->statusbar->clearMessage(); ui->statusbar->showMessage("Opened " + path);
} catch (const Botan::Invalid_Authentication_Tag &e) { } catch (const Botan::Invalid_Authentication_Tag &e) {
QMessageBox::critical(this, "Error", "Invalid password."); QMessageBox::critical(this, "Error", "Invalid password.");
ui->statusbar->clearMessage(); ui->statusbar->clearMessage();

View File

@@ -29,6 +29,25 @@ Vault::Vault(std::string path, const std::string &password)
Crypto::decrypt(m_key, check_ciphertext); Crypto::decrypt(m_key, check_ciphertext);
} }
void Vault::create(const std::string &path, const std::string &password) {
static Botan::AutoSeeded_RNG rng;
auto salt = rng.random_array<16>();
auto key = Crypto::derive_key(password, salt);
const std::string_view content = "LETSGO";
Botan::secure_vector<u8> content_sv(content.begin(), content.end());
auto check_ciphertext = Crypto::encrypt(key, content_sv);
std::ofstream create(path, std::ios::binary);
create.write("DULL", 4);
create.write(to_char_ptr(&VERSION), sizeof(VERSION));
create.write(to_char_ptr(salt.data()), 16);
create.write(to_char_ptr(check_ciphertext.data()), 46);
create.close();
}
std::vector<FileHeader> Vault::read_file_headers() { std::vector<FileHeader> Vault::read_file_headers() {
std::vector<FileHeader> headers; std::vector<FileHeader> headers;

View File

@@ -19,6 +19,8 @@ class Vault {
public: public:
explicit Vault(std::string path, const std::string &password); explicit Vault(std::string path, const std::string &password);
static void create(const std::string &path, const std::string &password);
std::vector<FileHeader> read_file_headers(); std::vector<FileHeader> read_file_headers();
std::optional<std::string> read_file(const std::string &name); std::optional<std::string> read_file(const std::string &name);
void create_file(const std::string &name, const std::string &content); void create_file(const std::string &name, const std::string &content);