retry on weak password, Vault::create
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
// TODO: actual fs
|
||||
#include "mainwindow.h"
|
||||
#include "crypto.h"
|
||||
#include <QDesktopServices>
|
||||
#include <QDropEvent>
|
||||
#include <QFileDialog>
|
||||
@@ -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<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();
|
||||
Vault::create(path.toStdString(), password.toStdString());
|
||||
|
||||
m_vault =
|
||||
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]() {
|
||||
@@ -95,7 +83,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||
m_vault =
|
||||
std::make_unique<Vault>(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();
|
||||
|
||||
19
src/vault.cc
19
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<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> headers;
|
||||
|
||||
|
||||
@@ -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<FileHeader> read_file_headers();
|
||||
std::optional<std::string> read_file(const std::string &name);
|
||||
void create_file(const std::string &name, const std::string &content);
|
||||
|
||||
Reference in New Issue
Block a user