diff --git a/src/mainwindow.cc b/src/mainwindow.cc index beced93..41d136d 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -1,6 +1,5 @@ // TODO: actual fs #include "mainwindow.h" -#include "crypto.h" #include #include #include @@ -39,39 +38,28 @@ MainWindow::MainWindow(QWidget *parent) path += ".dull"; } - QString password = QInputDialog::getText( - this, "Choose a password", "Choose a password", QLineEdit::Password); - if (password.length() < 8) { - QMessageBox::critical(this, "Error", - "Password must be at least 8 characters long."); - return; + QString password; + while (true) { + password = QInputDialog::getText( + this, "Choose a password", "Choose a password", QLineEdit::Password); + if (password.length() < 8) { + QMessageBox::critical(this, "Error", + "Password must be at least 8 characters long."); + } else { + break; + } } ui->statusbar->showMessage("Creating the vault..."); QCoreApplication::processEvents(); - static Botan::AutoSeeded_RNG rng; - auto salt = rng.random_array<16>(); - - auto key = Crypto::derive_key(password.toStdString(), salt); - - const std::string_view content = "LETSGO"; - Botan::secure_vector 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(); + Vault::create(path.toStdString(), password.toStdString()); m_vault = std::make_unique(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]() { @@ -95,7 +83,7 @@ MainWindow::MainWindow(QWidget *parent) m_vault = std::make_unique(path.toStdString(), password.toStdString()); reload_fs_tree(); - ui->statusbar->clearMessage(); + ui->statusbar->showMessage("Opened " + path); } catch (const Botan::Invalid_Authentication_Tag &e) { QMessageBox::critical(this, "Error", "Invalid password."); ui->statusbar->clearMessage(); diff --git a/src/vault.cc b/src/vault.cc index c221176..c2531e6 100644 --- a/src/vault.cc +++ b/src/vault.cc @@ -29,6 +29,25 @@ Vault::Vault(std::string path, const std::string &password) 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 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 Vault::read_file_headers() { std::vector headers; diff --git a/src/vault.h b/src/vault.h index b89d958..d7e26fd 100644 --- a/src/vault.h +++ b/src/vault.h @@ -19,6 +19,8 @@ class Vault { public: explicit Vault(std::string path, const std::string &password); + static void create(const std::string &path, const std::string &password); + std::vector read_file_headers(); std::optional read_file(const std::string &name); void create_file(const std::string &name, const std::string &content);