-
Notifications
You must be signed in to change notification settings - Fork 0
/
storepassword.cpp
155 lines (123 loc) · 4.35 KB
/
storepassword.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "storePassword.h"
#include "ui_storePassword.h"
#include "CryptoUtils.h" // Include the utility
#include "mainwindow.h" // Include to access globalCipherKey
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <QDebug>
#include <QStandardPaths>
#include <QDir>
#include <QDateTime> // Include for handling date and time
#include <QCloseEvent>
// =====================
// Constructor & Destructor
// =====================
storePassword::storePassword(QWidget *parent)
: QDialog(parent), ui(new Ui::storePassword)
{
ui->setupUi(this);
setWindowTitle("Falkenberg's Password Manager");
}
storePassword::~storePassword()
{
delete ui;
}
void storePassword::closeEvent(QCloseEvent *event)
{
QCoreApplication::quit(); // Quit the entire application
event->accept(); // Accept the event, allowing the window to close
}
// =====================
// UI Signal Handlers
// =====================
void storePassword::on_backButton_clicked()
{
this->hide();
emit emitBackClicked(); // Emit signal to notify back button was clicked
clearUI();
this->accept(); // Close the dialog
}
void storePassword::clearUI(){
// Clear UI fields
ui->passIdText->clear();
ui->passwordText->clear();
ui->userText->clear();
ui->thoughtText->clear();
}
void storePassword::on_genPassButton_clicked()
{
emit requestGenPassword(); // Emit signal to request password generation
}
void storePassword::on_storePassButton_clicked()
{
QString passId = ui->passIdText->toPlainText();
QString password = ui->passwordText->toPlainText();
// Ensure passId and password fields are not empty
if (passId.isEmpty() || password.isEmpty()) {
return;
}
// Check if passId already exists
if (doesPassIdExist(passId)) {
// Display an error message to the user
QMessageBox::warning(this, "Error", "A password with this ID already exists. Please choose a different ID. If you can't see the password ID on the password viewer, then it was used with a different cipher.");
return;
}
// Get the current date and time as a string
QString dateStored = QDate::currentDate().toString(Qt::ISODate);
// Create JSON object with user input data and the current date
QJsonObject json;
json["passId"] = passId;
json["password"] = password;
json["username"] = ui->userText->toPlainText();
json["thoughts"] = ui->thoughtText->toPlainText();
json["dateStored"] = dateStored; // Add the date to the JSON object
QJsonDocument doc(json);
QByteArray jsonData = doc.toJson();
// Encrypt the JSON data using CryptoUtils with globalCipherKey from MainWindow
QByteArray encryptedData = CryptoUtils::encryptData(jsonData, globalCipherKey);
// Save the encrypted data to a file
saveEncryptedDataToFile(passId, encryptedData);
clearUI();
this->hide();
emit emitBackClicked();
this->accept();
}
// =====================
// File Handling Functions
// =====================
void storePassword::saveEncryptedDataToFile(const QString &passId, const QByteArray &encryptedData)
{
QString dirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/PasswordManager";
QString filePath = dirPath + "/" + passId + ".bin";
QDir dir(dirPath);
if (!dir.exists()) {
if (!dir.mkpath(".")) {
qWarning() << "Could not create directory:" << dirPath;
return;
}
}
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << "Could not open file for writing:" << filePath;
return;
}
QDataStream out(&file);
out << encryptedData;
file.close();
// Decrypt the data to verify it can be correctly decrypted
QByteArray decryptedData = CryptoUtils::decryptData(encryptedData, globalCipherKey);
// Optionally convert the decrypted data back to JSON for further verification
QJsonDocument decryptedDoc = QJsonDocument::fromJson(decryptedData);
if (decryptedDoc.isNull()) {
} else {
QJsonObject decryptedJson = decryptedDoc.object();
}
}
bool storePassword::doesPassIdExist(const QString &passId)
{
QString dirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/PasswordManager";
QString filePath = dirPath + "/" + passId + ".bin";
QFile file(filePath);
return file.exists();
}