-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordManager.cpp
More file actions
89 lines (71 loc) · 2.74 KB
/
Copy pathPasswordManager.cpp
File metadata and controls
89 lines (71 loc) · 2.74 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
#include<iostream>
#include<string>
#include<fstream>
#include "PasswordManager.h"
#include "FileHandler.h"
using namespace std;
void PasswordManager::setPassword() {
FileHandler fileHandler;
string newPassword;
string pass1, pass2;
bool doubleCheck = false;
char choice;
bool passwordExists = checkPasswordAvailability();
if (passwordExists) { // to check if there's old password already set
cout << "Do you want to change your password? (Y/N)\n";
cin >> choice;
if (choice == 'Y' || choice == 'y') {
do {
cout << "Please enter a new password:\n";
cin.ignore();
getline(cin, pass1);
cout << "Please enter your password again:\n";
getline(cin, pass2);
if (pass1 == pass2) {
cout << "Your password was changed successfully!\n";
newPassword = pass2;
password = newPassword;
doubleCheck = true;
fileHandler.setPassword(password); // passing the password to the file if it matches
cout << "Your password is: " << newPassword << "\nremember it well!\n";
}
else {
cout << "Passwords don't match, please re-enter your password:\n";
}
} while (!doubleCheck);
}
else {
cout << "Password won't be changed!" << endl;
}
}
else { // if this is the first password to be set
do {
cout << "Please enter your password:\n";
getline(cin, pass1);
cout << "Please enter your password again:\n"; // password checker
getline(cin, pass2);
if (pass1 == pass2) {
doubleCheck = true;
cout << "Your password was set successfully!\n";
newPassword= pass2;
password = newPassword;
fileHandler.setPassword(newPassword); // passing the password to the file if it matches
cout << "Your password is: " << newPassword << "\nremember it well!\n";
passwordExists = true;
}
else {
cout << "Passwords don't match, please re-enter your password:\n";
}
} while (!doubleCheck);
}
}
bool PasswordManager::getPassword(const string& pass) {
ifstream passCheck("password.txt");
getline(passCheck, password);
return (pass == password); /* to check if the password entered is the
same as the one stored in the file */
}
bool PasswordManager::checkPasswordAvailability() {
ifstream passFile("password.txt");
return passFile.peek() != ifstream::traits_type::eof();
}