-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAccountManager.cpp
More file actions
125 lines (99 loc) · 3.53 KB
/
AccountManager.cpp
File metadata and controls
125 lines (99 loc) · 3.53 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#define _CRT_SECURE_NO_WARNINGS
#include "./AccountManager.h"
#include "../utils/Base64.h"
#include "../utils/IOUtil.h"
#include "./Log.h"
#include <Windows.h>
#include <dpapi.h>
#include <rapidjson/error/en.h>
#include <rapidjson/prettywriter.h>
rapidjson::Value Account::dump(
rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> allocator) {
rapidjson::Value output(rapidjson::kObjectType);
output.AddMember("order", rapidjson::Value(order), allocator);
output.AddMember("color", rapidjson::Value(color.data(), color.size()),
allocator);
output.AddMember("username",
rapidjson::Value(username.data(), username.size()),
allocator);
output.AddMember("password",
rapidjson::Value(password.data(), password.size()),
allocator);
return output;
}
Account::Account() : order(0), color("#000000"), password("") {}
Account::Account(const rapidjson::Value &data)
: order(data["order"].GetInt()),
color(data["color"].GetString(), data["color"].GetStringLength()),
username(data["username"].GetString(),
data["username"].GetStringLength()),
password(data["password"].GetString(),
data["password"].GetStringLength()) {}
bool AccountManager::encrypt(std::string input, std::string &output) {
DATA_BLOB in;
DATA_BLOB out;
in.pbData = (BYTE *)input.data();
in.cbData = input.size();
if (CryptProtectData(&in, NULL, NULL, NULL, NULL, 0, &out)) {
std::string encrypted;
encrypted.resize(out.cbData);
memcpy(encrypted.data(), out.pbData, out.cbData);
output = Base64::Encode(encrypted);
return true;
}
return false;
}
bool AccountManager::decrypt(std::string input, std::string &output) {
std::string encrypted = Base64::Decode(input);
DATA_BLOB in;
DATA_BLOB out;
in.pbData = (BYTE *)encrypted.data();
in.cbData = encrypted.size();
if (CryptUnprotectData(&in, NULL, NULL, NULL, NULL, 0, &out)) {
output.resize(out.cbData);
memcpy(output.data(), out.pbData, out.cbData);
return true;
}
return false;
}
std::string AccountManager::dump() {
rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> allocator;
rapidjson::StringBuffer buffer;
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
dump(allocator).Accept(writer);
return {buffer.GetString(), buffer.GetSize()};
}
rapidjson::Value AccountManager::dump(
rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator> allocator) {
rapidjson::Value output(rapidjson::kArrayType);
for (auto &[name, acc] : data)
output.PushBack(acc.dump(allocator), allocator);
return output;
}
bool AccountManager::save() {
return IOUtil::writeFile(folder.directory + path, dump());
}
bool AccountManager::load() {
std::string read;
if (IOUtil::readFile(folder.directory + path, read)) {
rapidjson::Document document;
rapidjson::ParseResult ok = document.Parse(read.data(), read.size());
if (ok) {
// new format
if (document.IsArray())
for (rapidjson::Value::ValueIterator it = document.Begin();
it != document.End(); ++it) {
Account acc(*it);
data[acc.username] = acc;
}
else
clog::error << "Refusing to load outdated password data." << clog::endl;
} else {
clog::error << "Error parsing password data: "
<< GetParseError_En(ok.Code()) << " (" << ok.Offset() << ")"
<< clog::endl;
}
}
return true;
}
AccountManager::AccountManager(ClientFolder &f) : folder(f) {}