-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswords.cpp
More file actions
108 lines (94 loc) · 3.06 KB
/
Copy pathPasswords.cpp
File metadata and controls
108 lines (94 loc) · 3.06 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
#include "Passwords.h"
std::string EandD(const std::string& s, const std::string& key) { //Encryption and Decryption
std::string result = s;
for (size_t i = 0; i < s.size(); ++i)
result[i] = s[i] ^ key[i%(size(key)/sizeof(key[0]))];
return result;
}
void psw::Insert(const std::string& name, const std::string& login,
const std::string& password, const std::string& comment) {
if (allowLogin) {
std::string sql("INSERT into Passwords (User_ID, Name, Login, Password, Comment) VALUES "
"('" + std::to_string(User_ID) + "', '" + EandD(name, key) +
"', '" + EandD(login, key) + "', '" + EandD(password, key) +
"', '" + EandD(comment, key) + "');");
execute_sql(sql);
}
}
//update it
void psw::Update(const std::string& field, const std::string& value,
const std::string& where_field, const std::string& wvalue) {
if (allowLogin) {
if (where_field == "ID") {
std::string sql("UPDATE Passwords SET " + field + " = '" +
EandD(value, key) + "' WHERE " + where_field + " = '" +
wvalue + "';");
execute_sql(sql);
} else {
std::string sql("UPDATE Passwords SET " + field + " = '" +
EandD(value, key) + "' WHERE " + where_field + " = '" +
EandD(wvalue, key) + "';");
execute_sql(sql);
}
}
}
//update it
void psw::Select(const std::string& field, const std::string& where_field,
const std::string& where_value) {
if (allowLogin) {
std::string sql("SELECT " + field + "from Passwords WHERE " + where_field + " = '" +
EandD(where_value, key) + "';");
execute_sql(sql);
}
}
std::vector<std::string> psw::SelectAll() {
std::vector<std::string> pw = {}; //for passwords
if (allowLogin) {
std::string sql("SELECT Name, Login, Password from Passwords WHERE User_ID = '" + std::to_string(User_ID) + "';");
std::stringstream result = get_executed_sql(sql);
int a = 1;
while (result) {
std::string name, l, p; // Name, Login, Password
getline(result, name, '\'');
getline(result, l, '\"');
getline(result, p, '\n');
if (l.empty() || name.empty()) {
continue;
}
pw.push_back(EandD(p, key));
std::cout << a++ << " Name: " << EandD(name, key) << "\nLogin: "
<< EandD(l, key) << std::endl;
// std::cout << "Name: " << EandD(name, key) << "\nLogin: " << EandD(l, key)
// << "\nPassword: " << EandD(p, key) << '\n' << std::endl;
}
}
return pw;
}
//update it
void psw::Delete(const std::string& field, const std::string& value) {
if (allowLogin) {
std::string sql("DELETE from Passwords WHERE " + field + " = '" + value + "';");
execute_sql(sql);
}
}
int psw::getUser_ID() const {
if (allowLogin) {
return User_ID;
}
return 0;
}
psw::psw(const int& uid, const std::string& k, const bool allow)
: User_ID(uid), key(k), allowLogin(allow) {}
std::vector<int> psw::SelectID() {
std::string sql("SELECT ID from Passwords WHERE User_ID = '" + std::to_string(User_ID) + "';");
std::stringstream s = get_executed_sql2(sql);
std::vector<int> result;
while (s) {
std::string id;
getline(s, id, '/');
if (id.empty())
continue;
result.push_back(stoi(id));
}
return result;
}