-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpindialog.cpp
More file actions
236 lines (203 loc) · 7.47 KB
/
Copy pathpindialog.cpp
File metadata and controls
236 lines (203 loc) · 7.47 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "pindialog.h"
#include <QApplication>
#include <QCryptographicHash>
#include <QGraphicsDropShadowEffect>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSettings>
#include <QVBoxLayout>
// ── helpers ──────────────────────────────────────────────
static QString hashPin(const QString &pin) {
// SHA-256 with a fixed app salt
QByteArray salted = QByteArray("CipherVault::PIN::") + pin.toUtf8();
return QString::fromLatin1(
QCryptographicHash::hash(salted, QCryptographicHash::Sha256).toHex());
}
static void saveHash(const QString &hash) {
QSettings s;
s.setValue("security/pinHash", hash);
}
static QString loadHash() {
QSettings s;
return s.value("security/pinHash").toString();
}
// ── static API ───────────────────────────────────────────
bool PinDialog::isPinSet() {
return !loadHash().isEmpty();
}
bool PinDialog::verifyPin() {
PinDialog dlg(Verify);
return dlg.exec() == QDialog::Accepted;
}
bool PinDialog::setupPin() {
PinDialog dlg(Setup);
return dlg.exec() == QDialog::Accepted;
}
bool PinDialog::changePin(QWidget *parent) {
// First verify old PIN
PinDialog verify(Verify, parent);
verify.setWindowTitle("Verify Current PIN");
if (verify.exec() != QDialog::Accepted)
return false;
// Then set new one
PinDialog setup(Setup, parent);
setup.setWindowTitle("Set New PIN");
return setup.exec() == QDialog::Accepted;
}
// ── constructor ──────────────────────────────────────────
PinDialog::PinDialog(Mode mode, QWidget *parent)
: QDialog(parent), m_mode(mode) {
setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
setAttribute(Qt::WA_TranslucentBackground);
setFixedSize(420, 380);
buildUi();
}
void PinDialog::buildUi() {
auto *outer = new QVBoxLayout(this);
outer->setContentsMargins(0, 0, 0, 0);
// Card container
auto *card = new QWidget;
card->setObjectName("pinCard");
card->setStyleSheet(
"#pinCard {"
" background: qlineargradient(x1:0,y1:0,x2:0,y2:1,"
" stop:0 #0f1318, stop:1 #080c14);"
" border: 1px solid rgba(0, 212, 255, 0.25);"
" border-radius: 16px;"
"}");
auto *glow = new QGraphicsDropShadowEffect;
glow->setBlurRadius(40);
glow->setColor(QColor(0, 212, 255, 50));
glow->setOffset(0, 0);
card->setGraphicsEffect(glow);
auto *lay = new QVBoxLayout(card);
lay->setContentsMargins(36, 32, 36, 32);
lay->setSpacing(16);
lay->setAlignment(Qt::AlignCenter);
// Lock icon
auto *icon = new QLabel(QString::fromUtf8("🔐"));
icon->setAlignment(Qt::AlignCenter);
icon->setStyleSheet("font-size: 48px; padding: 0;");
lay->addWidget(icon);
// Title
m_title = new QLabel(m_mode == Setup ? "SET YOUR PIN" : "ENTER PIN");
m_title->setAlignment(Qt::AlignCenter);
m_title->setStyleSheet(
"font-size: 20px; font-weight: 800; color: #00d4ff;"
"letter-spacing: 4px;");
lay->addWidget(m_title);
// Subtitle
m_subtitle = new QLabel(
m_mode == Setup
? "Create a 4-8 digit PIN to secure the app"
: "Enter your PIN to unlock CipherVault");
m_subtitle->setAlignment(Qt::AlignCenter);
m_subtitle->setWordWrap(true);
m_subtitle->setStyleSheet("font-size: 12px; color: #7d8590;");
lay->addWidget(m_subtitle);
lay->addSpacing(8);
// PIN input
m_pinInput = new QLineEdit;
m_pinInput->setEchoMode(QLineEdit::Password);
m_pinInput->setMaxLength(8);
m_pinInput->setAlignment(Qt::AlignCenter);
m_pinInput->setPlaceholderText(m_mode == Setup ? "New PIN" : "PIN");
m_pinInput->setStyleSheet(
"QLineEdit {"
" background: #181c2f; border: 1px solid #2a3050;"
" border-radius: 8px; padding: 12px; color: #e6edf3;"
" font-size: 22px; letter-spacing: 12px;"
"}"
"QLineEdit:focus { border-color: #00d4ff; }");
lay->addWidget(m_pinInput);
// Confirm input (setup mode only)
m_pinConfirm = new QLineEdit;
m_pinConfirm->setEchoMode(QLineEdit::Password);
m_pinConfirm->setMaxLength(8);
m_pinConfirm->setAlignment(Qt::AlignCenter);
m_pinConfirm->setPlaceholderText("Confirm PIN");
m_pinConfirm->setStyleSheet(m_pinInput->styleSheet());
m_pinConfirm->setVisible(m_mode == Setup);
lay->addWidget(m_pinConfirm);
// Error label
m_errorLabel = new QLabel;
m_errorLabel->setAlignment(Qt::AlignCenter);
m_errorLabel->setStyleSheet("color: #ff4466; font-size: 12px; font-weight: 600;");
m_errorLabel->setVisible(false);
lay->addWidget(m_errorLabel);
// Submit button
m_submitBtn = new QPushButton(m_mode == Setup ? "SET PIN" : QString::fromUtf8("UNLOCK 🔓"));
m_submitBtn->setCursor(Qt::PointingHandCursor);
m_submitBtn->setMinimumHeight(44);
m_submitBtn->setStyleSheet(
"QPushButton {"
" background: qlineargradient(x1:0,y1:0,x2:1,y2:0,"
" stop:0 #00d4ff, stop:1 #00a8cc);"
" color: #000; font-weight: 700; font-size: 14px;"
" border: none; border-radius: 8px; padding: 10px 28px;"
" letter-spacing: 2px;"
"}"
"QPushButton:hover {"
" background: qlineargradient(x1:0,y1:0,x2:1,y2:0,"
" stop:0 #33e0ff, stop:1 #00d4ff);"
"}");
lay->addWidget(m_submitBtn);
// Skip button for setup only
if (m_mode == Setup) {
auto *skipBtn = new QPushButton("Skip for now");
skipBtn->setCursor(Qt::PointingHandCursor);
skipBtn->setStyleSheet(
"QPushButton { background: none; border: none; color: #3d4450;"
" font-size: 12px; }"
"QPushButton:hover { color: #7d8590; }");
connect(skipBtn, &QPushButton::clicked, this, [this]() {
accept(); // allow entry without setting PIN
});
lay->addWidget(skipBtn, 0, Qt::AlignCenter);
}
outer->addWidget(card);
// Connections
connect(m_submitBtn, &QPushButton::clicked, this, &PinDialog::onSubmit);
connect(m_pinInput, &QLineEdit::returnPressed, this, &PinDialog::onSubmit);
connect(m_pinConfirm, &QLineEdit::returnPressed, this, &PinDialog::onSubmit);
m_pinInput->setFocus();
}
void PinDialog::onSubmit() {
QString pin = m_pinInput->text();
if (pin.length() < 4) {
m_errorLabel->setText("PIN must be at least 4 digits");
m_errorLabel->setVisible(true);
return;
}
// Digits only
for (const auto &ch : pin) {
if (!ch.isDigit()) {
m_errorLabel->setText("PIN must contain only digits");
m_errorLabel->setVisible(true);
return;
}
}
if (m_mode == Setup) {
if (pin != m_pinConfirm->text()) {
m_errorLabel->setText("PINs do not match");
m_errorLabel->setVisible(true);
m_pinConfirm->clear();
m_pinConfirm->setFocus();
return;
}
saveHash(hashPin(pin));
accept();
} else {
// Verify
if (hashPin(pin) == loadHash()) {
accept();
} else {
m_errorLabel->setText("Incorrect PIN");
m_errorLabel->setVisible(true);
m_pinInput->clear();
m_pinInput->setFocus();
}
}
}