-
Notifications
You must be signed in to change notification settings - Fork 12
/
keychain.h
98 lines (81 loc) · 2.21 KB
/
keychain.h
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
#pragma once
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "json.hpp"
// for convenience
using json = nlohmann::json;
struct KeychainField {
std::string name;
std::string value;
std::string type;
bool password;
};
struct KeychainItem {
std::string title;
std::string uuid;
std::string category;
std::string folder;
std::unordered_map<std::string, std::vector<KeychainField>> sections;
void addField(std::string section, KeychainField field) {
auto it = sections.find(section);
if (it == sections.end()) {
sections.emplace(std::make_pair(section, std::vector<KeychainField>{field}));
} else {
it->second.push_back(std::move(field));
}
}
std::vector<std::string> URLs;
std::string website;
std::string notes;
};
class AgileKeychainMasterKey {
public:
AgileKeychainMasterKey(const json& input, const std::string masterPassword);
json decryptItem(const json& input);
json decryptJSON(const std::string& input);
std::string encryptJSON(const json& input);
std::string level;
std::string id;
private:
std::vector<uint8_t> key_data;
};
class Keychain {
public:
Keychain(std::string path, std::string masterPassword);
using ItemMap = std::unordered_map<std::string, KeychainItem>;
ItemMap::iterator begin() {
if (!loaded)
reloadItems();
return items.begin();
}
ItemMap::iterator end() {
if (!loaded)
reloadItems();
return items.end();
}
ItemMap::iterator find(std::string key) {
if (!loaded)
reloadItems();
return items.find(key);
}
void reloadItems();
void unloadItems();
std::string getTitle() {
return title;
}
json decryptJSON(const std::string& input) {
return level5_key->decryptJSON(input);
}
std::string encryptJSON(const json& input) {
return level5_key->encryptJSON(input);
}
private:
void loadItem(std::string uuid);
ItemMap items;
std::unique_ptr<AgileKeychainMasterKey> level3_key, level5_key;
std::string vault_path;
std::string title;
bool loaded = false;
};