-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncryption.hpp
164 lines (125 loc) · 4.67 KB
/
Encryption.hpp
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#pragma once
#include <memory>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <chrono>
#include <ctime>
/*
* Holds information used for encrypting a message.
* @values:
* symmetricKey: encrypted sym key used for AEAD of cipher text.
* nonce: one time random number used in encryption of cipher text.
*/
struct EncryptionData
{
EncryptionData() = default;
~EncryptionData() = default;
template<typename T>
EncryptionData(T&& _key, T&& _nonce)
:symmetricKey( std::forward<T>(_key) ),
nonce( std::forward<T>(_nonce) )
{}
std::vector<uint8_t> ciphertext; // Encrypted plain text:
std::vector<uint8_t> symmetricKey; // Key used in encryption:
std::vector<uint8_t> nonce; // Random number used in encryption:
};
class EncryptionWrapper{
public:
/* Constructor */
EncryptionWrapper();
/* Destructor */
~EncryptionWrapper();
/* Create user, encrypts password and stores it in a file */
template<typename T, typename K>
void createUser(T&& username, K&& plainPassword);
/* Generate a private key */
std::vector<std::string> generateKeyPair()noexcept;
/* Encrypt a users password */
std::string passwordEncryption(const std::string& password)const;
/* Check password with a given hash */
bool passwordChecker(const std::string& password, const std::string& hash)const;
/* Encrypts users message */
std::string messageEncryption(const std::string& plaintext, const std::string& publickey);
/* Decrypts recieved message */
std::string messageDecryption(const std::string& ciphertext, const std::string& privatekey);
/* Sets encryption data created from cipher text */
void setFEdata(std::string&& symmetric_key, std::string&& nonce);
void setFEdata(std::vector<uint8_t>&& symmetric_key, std::vector<uint8_t>&& nonce);
/* Grabs symmetric key from most recent cipher text */
std::unique_ptr<EncryptionData> getEncryptionData();
/* Sets data used in encryption of cipher text, to be decrypted */
void setEncryptionData(std::unique_ptr<EncryptionData> _feData);
/* Toggle optional logging */
void logging();
private:
/* Container to hold data from encryption */
std::unique_ptr<EncryptionData> eData;
/* Encryption data from cipher text to be decrypted */
std::unique_ptr<EncryptionData> feData;
/* Log function calls */
template<typename T>
void logAndProccess(T&& param)const;
/* Toggle optional logging */
bool logFunctions{false};
};
/* Template Functions */
/*
* Associates a username with an encrpyted password and stores information:
* @params: _username{ type string}.
* plainPassword{type string} password to be encrpyted.
* @returns: if new user adds username and encrpyted password to file.
*/
template<typename T, typename K>
void EncryptionWrapper::createUser(T&& _username, K&& _plainPassword)
{
// Perfect forward to avoid having multiple copies around:
std::string username{std::forward<T>(_username)};
std::string password{std::forward<K>(_plainPassword)};
std::fstream fs("passwords", std::fstream::in | std::fstream::out | std::fstream::app);
std::string line;
// Iterate file and grab usernames:
while(std::getline(fs, line))
{
auto found = line.find(' ');
if(found != std::string::npos)
{
// If user alredy exist warn and exit:
if(line.compare(0, found, username) == 0)
{
std::cout << "User " << username << " already exist" << std::endl;
return;
}
}
}
// User does not exist therefore, create and store hashed password:
if(fs.is_open())
{
// Reset file pointer:
fs.clear();
fs << username << ' ' << passwordEncryption(password) << '\n';
std::cout << "Added user: " << username << std::endl;
fs.close();
}
// log function call:
logAndProccess(__PRETTY_FUNCTION__);
}
/*
* Takes in a universal refrences and logs param.
* @warning: not thread safe.
* @params: name of subject to be logged.
* @retuns: writes current date and subject to file.
*/
template<typename T>
void EncryptionWrapper::logAndProccess(T&& param)const
{
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
std::ofstream fs;
fs.open("functionLogs", std::fstream::out | std::ofstream::app);
// Format and write to buffer:
fs << ctime(&time) << ' ' << param << '\n';
// Write to file and close:
fs.close();
}