-
Notifications
You must be signed in to change notification settings - Fork 0
/
sproj5.cpp
247 lines (194 loc) · 5.09 KB
/
sproj5.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
// Benjamin Hurley
// proj5.cpp (driver)
#include <iostream>
#include "passserver.h"
#include <cstring>
// libraries for password hiding
#include <string>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
using namespace std;
using namespace hashProject;
void Menu();
// functions for password hiding
string hidePassword(const char* prompt, bool show_asterisk=true);
int getch();
int main()
{
char choice;
size_t size;
cout << "\n";
cout << "--------------------------------------" << endl;
cout << "|| CREDENTIALS MANAGER - SIMULATION ||" << endl;
cout << "--------------------------------------" << endl;
cout << "\n";
cout << "Welcome. Enter preferred Hash Table capacity to begin: ";
cin >> size;
PassServer p(size);
do
{
cout << endl;
Menu();
cin >> choice;
if (choice == 'l')
{
// load from file into hash table
string file;
bool result;
cout << "Enter password file name to load from: ";
cin >> file;
const char * filename = file.c_str();
result = p.load(filename);
if (result == false) cout << "Error" << endl;
}
else if (choice == 'a')
{
// add user
string username;
string password;
cout << "Enter new Username: ";
cin >> username;
cin.ignore();
password = hidePassword("Enter new Password: ",true);
// create pair
pair<string, string> x;
x.first = username;
x.second = password;
bool result = p.addUser(x);
if (result == false) cout << "Error" << endl;
else cout << "User Added" << endl;
}
else if (choice == 'r')
{
// remove user
string username;
cout << "Enter Username: ";
cin >> username;
// stub: would like to add password check before removing
bool result = p.removeUser(username);
if (result == false) cout << "Error" << endl;
else cout << "User Removed" << endl;
}
else if (choice == 'c')
{
// change user password
string username;
string oldPassword;
string newPassword;
pair<string, string> x;
cout << "Enter Username: ";
cin >> username;
x.first = username;
cin.ignore();
oldPassword = hidePassword("Enter Current Password: ",true);
x.second = oldPassword;
newPassword = hidePassword("Enter New Password: ",true);
bool result = p.changePassword(x, newPassword);
if (result == false) cout << "Error" << endl;
}
else if (choice == 'f')
{
// find user
string username;
cout << "Enter Username: ";
cin >> username;
bool result = p.find(username);
if (result == false) cout << "Could not find user." << endl;
else cout << "Username '" << username << "' Has Been Found." << endl;
}
else if (choice == 'd')
{
// dump hashtable
p.dump();
}
else if (choice == 's')
{
cout << "Size (in entries) of Hash Table: " << p.size() << endl;
}
else if (choice == 'w')
{
// write to file
string filename;
cout << "Enter Filename: ";
cin >> filename;
const char* changed = filename.c_str();
bool result = p.write_to_file(changed);
if (result == false) cout << "Error" << endl;
else cout << "Info Written to file Successfully" << endl;
}
else if (choice != 'x')
{
// invalid
cout << "*** Invalid Entry ***" << endl;
cout << endl;
}
}while(choice != 'x');
// exit
cout << "Terminating Simulation..." << endl;
return 0;
}
// Below function was provided by University
// The functions thay follow are sources, and are
// used to hide keyboard input from console for passwords.
void Menu()
{
cout << "\n";
cout << "--------------------------------" << endl;
cout << "|| Main Menu ||" << endl;
cout << "--------------------------------" << endl;
cout << "l - Load User Data from File" << endl;
cout << "a - Add User" << endl;
cout << "r - Remove User" << endl;
cout << "c - Change User Password" << endl;
cout << "f - Find User" << endl;
cout << "d - Dump HashTable" << endl;
cout << "s - HashTable Size" << endl;
cout << "w - Write to Password File" << endl;
cout << "x - Exit program" << endl;
cout << "\nEnter choice : ";
}
// hidePassword function for password hiding
// Code Source: http://www.cplusplus.com/articles/E6vU7k9E/
string hidePassword(const char *prompt, bool show_asterisk)
{
const char BACKSPACE=127;
const char RETURN=10;
string password;
unsigned char ch=0;
show_asterisk = true;
cout << prompt;
while((ch=getch())!=RETURN)
{
if(ch==BACKSPACE)
{
if(password.length()!=0)
{
if(show_asterisk)
cout <<"\b \b";
password.resize(password.length()-1);
}
}
else
{
password+=ch;
if(show_asterisk)
cout <<'*';
}
}
cout <<endl;
return password;
}
// getch function for password hiding
// Code Source: http://www.cplusplus.com/articles/E6vU7k9E/
int getch() {
int ch;
struct termios t_old, t_new;
tcgetattr(STDIN_FILENO, &t_old);
t_new = t_old;
t_new.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &t_new);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
return ch;
}