-
Notifications
You must be signed in to change notification settings - Fork 0
/
authenticator.cpp
129 lines (115 loc) · 4.07 KB
/
authenticator.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
#include "authenticator.h"
// authenticator.cpp
//std::string password_filename = passwordsPath;
// 构造函数,可以选择从文件加载密码本
PasswordAuthenticator::PasswordAuthenticator(std::string folderPath, std::string filename) {
loadPasswordMapFromFile(folderPath, filename);
}
// 增加用户和密码到密码本
void PasswordAuthenticator::addEntry(const std::string& userId, const std::string& password) {
passwordMap[userId] = password;
}
// 输出所有键值对
void PasswordAuthenticator::listEntries() const {
for (const auto& entry : passwordMap) {
std::cout << "User ID: " << entry.first << ", Password: " << entry.second << std::endl;
}
}
// 检查密码是否正确
bool PasswordAuthenticator::authenticate(const std::string& userId, const std::string& password) const {
auto it = passwordMap.find(userId);
if (it != passwordMap.end() && it->second == password) {
return true;
}
return false;
}
// 保存密码本到CSV文件
void PasswordAuthenticator::savePasswordMapToFile(std::string folderPath, const std::string& filename) {
std::ofstream file(folderPath + filename);
if (file.is_open()) {
for (const auto& entry : passwordMap) {
file << entry.first << ',' << entry.second << std::endl;
}
file.close();
}
else {
std::cerr << "Unable to open the file for saving." << std::endl;
}
}
// 从CSV文件加载密码本
void PasswordAuthenticator::loadPasswordMapFromFile(std::string folderPath, const std::string& filename) {
// Check if the file exists, if not, create it
std::ifstream fileCheck(folderPath + filename);
if (!fileCheck) {
std::ofstream createFile(folderPath + filename);
if (!createFile) {
std::cerr << "Error: creating file failed" << std::endl;
std::exit(1);
}
createFile.close();
}
fileCheck.close();
std::ifstream file(folderPath + filename);
if (file.is_open()) {
passwordMap.clear(); // 清空已有的密码映射
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string userId, password;
// 使用 ',' 作为分隔符
if (std::getline(iss, userId, ',') && std::getline(iss, password, ',')) {
// 移除 password 末尾的回车符
if (!password.empty() && password.back() == '\r') {
password.pop_back();
}
passwordMap[userId] = password;
}
}
file.close();
}
else {
std::cerr << "Unable to open the file for loading." << std::endl;
}
}
// void PasswordAuthenticator::loadPasswordMapFromFile(const std::string& filename) {
// std::ifstream file(filename);
// if (file.is_open()) {
// passwordMap.clear(); // ������е����뱾
// std::string line;
// while (std::getline(file, line)) {
// std::istringstream iss(line);
// std::string userId, password;
// if (std::getline(iss, userId, ',') && std::getline(iss, password)) {
// passwordMap[userId] = password;
// }
// }
// file.close();
// }
// else {
// std::cerr << "Unable to open the file for loading." << std::endl;
// }
// }
void PasswordAuthenticator::modifyEntry(const std::string& userId, const std::string& newPassword) {
auto it = passwordMap.find(userId);
if (it != passwordMap.end()) {
// Modify the password for the given user
it->second = newPassword;
std::cout << "Password for user ID " << userId << " modified successfully." << std::endl;
}
else {
std::cerr << "User ID " << userId << " not found. Unable to modify password." << std::endl;
}
}
bool userAuthentic(const std::string& enterdUserID, const std::string& enteredUserPwd, std::string folderPath, std::string filename) {
// 载入最新密码本
PasswordAuthenticator authenticator;
authenticator.loadPasswordMapFromFile(folderPath,filename);
if (authenticator.authenticate(enterdUserID, enteredUserPwd)) {
std::cout << "authentication successful." << std::endl;
return true;
}
else {
std::cout << "authentication failed." << std::endl;
return false;
}
}