-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
354 lines (339 loc) · 13 KB
/
main.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// First major update -> Adding classes, and allowing for multiple users.
// g++17 compile command: g++ -std=c++17 main.cpp User.cpp -lsqlite3 -o main
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <regex>
#include "User.hpp"
#include "sql/sqlite3.h"
using namespace std;
string escapeString(const string& input) {
string escapedString;
for (char c : input) {
switch (c) {
case '\'':
escapedString += "''"; // Replace single quote with two single quotes
break;
case '\"':
escapedString += "\\\""; // Replace double quote with backslash and double quote
break;
case '\\':
escapedString += "\\\\"; // Replace backslash with double backslash
break;
case '\0':
escapedString += "\\0"; // Replace null character with backslash and zero
break;
case '\b':
escapedString += "\\b"; // Replace backspace with backslash and b
break;
case '\n':
escapedString += "\\n"; // Replace newline with backslash and n
break;
case '\r':
escapedString += "\\r"; // Replace carriage return with backslash and r
break;
case '\t':
escapedString += "\\t"; // Replace tab with backslash and t
break;
case '\x1A':
escapedString += "\\Z"; // Replace Ctrl+Z (substitute) with backslash and Z
break;
// Add more cases for other special characters if needed
default:
escapedString += c;
}
}
return escapedString;
}
bool createUserTable(){
sqlite3 *db;
int res = sqlite3_open("passwordManager.db", &db);
if(res != SQLITE_OK){
cout << "Error opening database" << endl;
return false;
}
const char* userTableQuery = "CREATE TABLE IF NOT EXISTS users ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"username TEXT NOT NULL,"
"email TEXT NOT NULL,"
"hash TEXT NOT NULL);";
res = sqlite3_exec(db, userTableQuery, NULL, NULL, NULL);
if(res != SQLITE_OK){
cout << "Error creating table: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return false;
}
sqlite3_close(db);
return true;
}
bool createPasswordTable(){
sqlite3 *db;
int res = sqlite3_open("passwordManager.db", &db);
if(res != SQLITE_OK){
cout << "Error opening database" << endl;
return false;
}
const char* passwordTableQuery = "CREATE TABLE IF NOT EXISTS passwords (id INTEGER PRIMARY KEY AUTOINCREMENT,userID INTEGER NOT NULL,service TEXT NOT NULL,username TEXT NOT NULL,encrypted_password TEXT NOT NULL,FOREIGN KEY (userID) REFERENCES users (id));";
res = sqlite3_exec(db, passwordTableQuery, NULL, NULL, NULL);
if(res != SQLITE_OK){
cout << "Error creating table: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return false;
}
sqlite3_close(db);
return true;
}
bool usernameExists(string username){ // checks the Users table
sqlite3 *db;
int res = sqlite3_open("passwordManager.db", &db);
if(res != SQLITE_OK){
cout << "Error opening database" << endl;
return false;
}
string query = "SELECT * FROM users WHERE username = :username;";
sqlite3_stmt *stmt; // prepared statement
res = sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, NULL); // -1 means query is null terminated, stmt is the prepared statement
if(res != SQLITE_OK){ // if there is an error
cout << "Error preparing statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return false;
}
res = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":username"), username.c_str(), -1, SQLITE_STATIC);
if (res != SQLITE_OK) {
cout << "Error binding username parameter: " << sqlite3_errmsg(db) << endl;
sqlite3_finalize(stmt);
sqlite3_close(db);
return false;
}
res = sqlite3_step(stmt); // execute the prepared statement
if(res == SQLITE_ROW){ // if there is a row, then the username exists
sqlite3_finalize(stmt); // finalize the prepared statement
sqlite3_close(db); // close the database
return true; // return true
}
sqlite3_finalize(stmt); // finalize the prepared statement
sqlite3_close(db);// close the database
return false; // return false
}
bool emailExists(string email){
sqlite3 *db;
int res = sqlite3_open("passwordManager.db", &db);
if(res != SQLITE_OK){
cout << "Error opening database" << endl;
return false;
}
string query = "SELECT * FROM users WHERE email = :email;";
sqlite3_stmt *stmt; // prepared statement
res = sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, NULL); // -1 means query is null terminated, stmt is the prepared statement
if(res != SQLITE_OK){ // if there is an error
cout << "Error preparing statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return false;
}
// Bind the parameter to the prepared statement using named placeholder
res = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":email"), email.c_str(), -1, SQLITE_STATIC);
if (res != SQLITE_OK) {
cout << "Error binding email parameter: " << sqlite3_errmsg(db) << endl;
sqlite3_finalize(stmt);
sqlite3_close(db);
return false;
}
res = sqlite3_step(stmt); // execute the prepared statement
if(res == SQLITE_ROW){ // if there is a row, then the username exists
sqlite3_finalize(stmt); // finalize the prepared statement
sqlite3_close(db); // close the database
return true; // return true
}
sqlite3_finalize(stmt); // finalize the prepared statement
sqlite3_close(db);// close the database
return false; // return false
}
bool validEmail(string email){ // regex check, found on stackoverflow
const regex pattern("(\\w+)(\\.|_)?(\\w*)@(\\w+)(\\.(\\w+))+");
return regex_match(email,pattern);
}
string crunchPass(string primary_pass){
long hashed = 0;
string reversed;
int dig_count = 0;
for (int i = 0; i < primary_pass.length(); i++){// Hash the user's input password and store it in the 'hashed' variable
if(i != primary_pass.length() - 1){
hashed += primary_pass[i] * primary_pass[i+1];
hashed += primary_pass[i] * primary_pass[i] * primary_pass[i];
}else{
hashed += primary_pass[i] * primary_pass[0]; // arbitrary value for last dig mult
}
}
while (hashed > 0){ // Convert the 'hashed' value into a string i guess
int temp = 0;
temp = hashed % 10;
hashed = hashed / 10;
char current = char(temp + 48);
reversed += current;
dig_count++;
}
// reverse the string, return as long
string to_write;
for (int i = (dig_count-1); i >= 0; i--){
to_write += reversed[i];
}
return to_write;
}
bool newUser(string username, string email, string hash){
sqlite3 *db;
int res = sqlite3_open("passwordManager.db", &db);
if(res != SQLITE_OK){
cout << "Error opening database" << endl;
return false;
} // db is open successfully
string query = "INSERT INTO users (username, email, hash) VALUES (:username, :email, :hash);";
sqlite3_stmt *stmt; // prepared statement
res = sqlite3_prepare_v2(db, query.c_str(), -1, &stmt, NULL); // -1 means query is null terminated, stmt is the prepared statement
if(res != SQLITE_OK){ // if there is an error
cout << "Error preparing statement: " << sqlite3_errmsg(db) << endl;
sqlite3_close(db);
return false;
}
// Bind the parameters to the prepared statement using named placeholders
res = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":username"), username.c_str(), -1, SQLITE_STATIC);
if (res != SQLITE_OK) {
cout << "Error binding username parameter: " << sqlite3_errmsg(db) << endl;
sqlite3_finalize(stmt);
sqlite3_close(db);
return false;
}
res = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":email"), email.c_str(), -1, SQLITE_STATIC);
if (res != SQLITE_OK) {
cout << "Error binding email parameter: " << sqlite3_errmsg(db) << endl;
sqlite3_finalize(stmt);
sqlite3_close(db);
return false;
}
res = sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":hash"), hash.c_str(), -1, SQLITE_STATIC);
if (res != SQLITE_OK) {
cout << "Error binding hash parameter: " << sqlite3_errmsg(db) << endl;
sqlite3_finalize(stmt);
sqlite3_close(db);
return false;
}
res = sqlite3_step(stmt); // execute the prepared statement
if(res != SQLITE_DONE){ // if result is not done then there is an error
cout << "Error inserting user: " << sqlite3_errmsg(db) << endl;
sqlite3_finalize(stmt); // finalize the prepared statement
sqlite3_close(db); // close the database
return false; // return true
}
sqlite3_finalize(stmt); // finalize the prepared statement
sqlite3_close(db);// close the database
return true; // return true, since user was inserted
}
int main(){
createUserTable();
createPasswordTable();
User current = User();
bool signedIn = false;
while(!signedIn){
cout << "Welcome to the Password Manager!" << endl;
cout << "1. Create a new user\n2. Sign-in" << endl;
int choice;
cin >> choice;
if(choice == 1){
cout << "Enter a username: ";
string username;
cin >> username;
if(username.length() > 20){
cout << "Username too long" << endl;
return 1;
}
if(usernameExists(username)){
cout << "Username already exists" << endl;
return 1;
}
cout << "Enter your email: ";
string email;
cin >> email;
if(emailExists(email)){
cout << "Email already exists" << endl;
return 1;
}
if(!validEmail(email)){
cout << "Invalid email" << endl;
return 1;
}
cout << "Enter a master password: ";
string master;
cin >> master;
string hash = crunchPass(master);
if(!newUser(username, email, hash)){
cout << "Error creating user" << endl;
return 1;
}
}else if(choice == 2){
cout << "Enter your username: ";
string username;
cin >> username;
if(!usernameExists(username)){
cout << "Username does not exist" << endl;
return 1;
}
cout << "Enter your master password: ";
string master;
cin >> master;
current = User(username, master);
if(current.verifyUser()){
signedIn = true;
}else{
cout << "Invalid username or password" << endl;
return 1;
}
}else{
cout << "Invalid choice" << endl;
return 1;
}
}
while(1){ // main loop, access has been verified, and current user object instantiated
cout << "1. Add a new password\n2. View a specific password\n3. View all passwords\n4. Delete a password\n5. Exit" << endl;
int choice;
cin >> choice;
if(choice == 1){
cout << "Enter the service: ";
string service;
cin >> service;
cout << "Enter the username: ";
string username;
cin >> username;
cout << "Enter the password: ";
string password;
cin >> password;
service = escapeString(service);
username = escapeString(username);
password = escapeString(password);
current.addPassword(service, username, password);
}else if(choice == 2){
cout << "Enter the service: ";
string service;
cin >> service;
service = escapeString(service);
current.getServicePasswords(service);
}else if(choice == 3){
current.printAllPasswords();
}else if(choice == 4){
cout << "Enter the service: ";
string service;
cin >> service;
cout << "Enter the username: ";
string username;
cin >> username;
service = escapeString(service);
username = escapeString(username);
current.deletePassword(service, username);
}else if(choice == 5){
return 0;
}else{
cout << "Invalid choice" << endl;
cin.clear();
}
}
return 0;
}