1+ #include " key.h"
2+
13#include < filesystem>
24#include < fstream>
35#include < string>
46
5- #include " key.h"
6-
77std::filesystem::path key::keysPath () {
88 // Navigate to the root directory of the project
99 std::filesystem::path programRootPath = std::filesystem::current_path ().parent_path ();
@@ -13,51 +13,61 @@ std::filesystem::path key::keysPath() {
1313
1414int key::keyExists (std::string name) {
1515 // TODO fix function not case sensitive
16- /* 0: key doesn't exist
17- * 1: only publicKey exists
18- * 2: only privateKey exists
19- * 3: both keys exists */
16+ // 0: key doesn't exist
17+ // 1: only publicKey exists
18+ // 2: only privateKey exists
19+ // 3: both keys exists
2020 int status = NONE;
2121
2222 std::filesystem::path keysFolder = keysPath ();
2323
2424 // go over every file in the KEY_FOLDER
25- for (const auto & entry : std::filesystem::directory_iterator (keysFolder)) {
25+ for (const auto & entry : std::filesystem::directory_iterator (keysFolder)) {
2626 // check if the name matches
2727 if (entry.path ().stem ().string () == name) {
2828 // check if it's a public key
2929 if (entry.path ().extension () == " .pub" ) {
3030 // break the loop if both files are found
31- if (status == NONE) status = PUBLIC; else {status = BOTH; break ;};
31+ if (status == NONE)
32+ status = PUBLIC;
33+ else {
34+ status = BOTH;
35+ break ;
36+ };
3237 }
3338 // check if it's a private key
3439 else if (entry.path ().has_extension () == false ) {
35- if (status == NONE) status = PRIVATE; else {status = BOTH; break ;};
40+ if (status == NONE)
41+ status = PRIVATE;
42+ else {
43+ status = BOTH;
44+ break ;
45+ };
3646 }
3747 }
3848 }
3949
4050 return status;
4151}
4252
43- int key::writeKey (const std::string& name, std::vector<uint8_t > *data, const bool isPublic) {
53+ int key::writeKey (const std::string & name, std::vector<uint8_t > *data, const bool isPublic) {
4454 if (data == nullptr ) return -1 ;
4555
4656 std::filesystem::path keysFolder = keysPath ();
4757
48- std::filesystem::path keyFile = keysFolder / (name + (isPublic? " .pub" : " " ));
58+ std::filesystem::path keyFile = keysFolder / (name + (isPublic ? " .pub" : " " ));
4959 // load file in memory
5060 std::ofstream outFile (keyFile);
5161
5262 // key in base64 format
5363 std::string base64Data = base64Encode (*data);
5464
5565 if (outFile.is_open ()) {
56- outFile << " -----BEGIN RSA " << (isPublic? " PUBLIC" : " PRIVATE" ) << " KEY-----\n " ;
66+ outFile << " -----BEGIN RSA " << (isPublic ? " PUBLIC" : " PRIVATE" ) << " KEY-----\n " ;
5767 for (int i = 0 ; i < base64Data.size (); i += 64 ) {
5868 outFile << base64Data.substr (i, 64 ) << " \n " ;
5969 }
60- outFile << " -----END RSA " << (isPublic? " PUBLIC" : " PRIVATE" ) << " KEY-----\n " ;
70+ outFile << " -----END RSA " << (isPublic ? " PUBLIC" : " PRIVATE" ) << " KEY-----\n " ;
6171 outFile.close ();
6272 } else {
6373 std::cerr << " Could not write to file: " << keyFile << std::endl;
@@ -70,7 +80,7 @@ int key::writeKey(const std::string& name, std::vector<uint8_t> *data, const boo
7080int key::readKey (const std::string &name, std::vector<uint8_t > *data, bool isPublic) {
7181 std::filesystem::path keysFolder = keysPath ();
7282
73- std::filesystem::path keyFile = keysFolder / (name + (isPublic? " .pub" : " " ));
83+ std::filesystem::path keyFile = keysFolder / (name + (isPublic ? " .pub" : " " ));
7484 // load file in memory
7585 std::ifstream inFile (keyFile);
7686
@@ -83,8 +93,7 @@ int key::readKey(const std::string &name, std::vector<uint8_t> *data, bool isPub
8393 std::string currentLine;
8494 std::string keyString;
8595
86- while (getline (inFile, currentLine)) {
87-
96+ while (getline (inFile, currentLine)) {
8897 if (currentLine.empty ()) continue ;
8998 if (currentLine.at (0 ) == ' -' ) continue ;
9099
@@ -111,7 +120,6 @@ std::string key::base64Encode(const std::vector<uint8_t> &data) {
111120 int index = 0 ;
112121
113122 while (index < data.size ()) {
114-
115123 uint32_t dataSegment = 0 ;
116124
117125 // take 3 numbers from the vector
@@ -143,7 +151,7 @@ std::string key::base64Encode(const std::vector<uint8_t> &data) {
143151std::vector<uint8_t > key::base64Decode (std::string data) {
144152 // TODO the decoding might not work if the data size is not dividable by 4
145153 if (data.size () % 4 != 0 ) std::cerr << " Decoding data size not dividable by 4" << std::endl;
146-
154+
147155 std::vector<uint8_t > result;
148156
149157 while (!data.empty ()) {
@@ -162,7 +170,8 @@ std::vector<uint8_t> key::base64Decode(std::string data) {
162170
163171 // check for valid char code
164172 if (number > 0b111111 ) {
165- std::cerr << " trying to Decode not valid char: " << letterSegment.at (i) << std::endl;
173+ std::cerr << " trying to Decode not valid char: " << letterSegment.at (i)
174+ << std::endl;
166175 continue ;
167176 }
168177 dataSegment += number << i * 6 ;
@@ -187,24 +196,14 @@ uint8_t key::getBase64Index(char letter) {
187196}
188197
189198int key::createKey (std::vector<uint8_t > *keyPublic, std::vector<uint8_t > *keyPrivate) {
190- *keyPublic = {
191- 23 , 87 , 45 , 190 , 12 , 78 , 34 , 210 , 56 , 89 ,
192- 123 , 67 , 90 , 150 , 32 , 76 , 54 , 200 , 11 , 99 ,
193- 101 , 145 , 67 , 189 , 43 , 88 , 29 , 176 , 58 , 92 ,
194- 111 , 134 , 78 , 201 , 15 , 84 , 39 , 220 , 66 , 97 ,
195- 105 , 142 , 71 , 185 , 49 , 81 , 27 , 170 , 53 , 95 ,
196- 41
197- };
198- *keyPrivate = {
199- 34 , 78 , 123 , 56 , 89 , 210 , 45 , 190 , 12 , 87 ,
200- 67 , 150 , 32 , 76 , 54 , 200 , 11 , 99 , 101 , 145 ,
201- 67 , 189 , 43 , 88 , 29 , 176 , 58 , 92 , 111 , 134 ,
202- 78 , 201 , 15 , 84 , 39 , 220 , 66 , 97 , 105 , 142 ,
203- 71 , 185 , 49 , 81 , 27 , 170 , 53 , 95 , 102 , 147 ,
204- 68 , 191 , 44 , 85 , 30 , 177 , 59 , 93 , 112 , 135 ,
205- 79 , 202 , 16 , 85 , 40 , 221 , 67 , 98 , 23 , 87 ,
206- 91 , 65
207- };
199+ *keyPublic = {23 , 87 , 45 , 190 , 12 , 78 , 34 , 210 , 56 , 89 , 123 , 67 , 90 , 150 , 32 , 76 , 54 ,
200+ 200 , 11 , 99 , 101 , 145 , 67 , 189 , 43 , 88 , 29 , 176 , 58 , 92 , 111 , 134 , 78 , 201 ,
201+ 15 , 84 , 39 , 220 , 66 , 97 , 105 , 142 , 71 , 185 , 49 , 81 , 27 , 170 , 53 , 95 , 41 };
202+ *keyPrivate = {34 , 78 , 123 , 56 , 89 , 210 , 45 , 190 , 12 , 87 , 67 , 150 , 32 , 76 , 54 ,
203+ 200 , 11 , 99 , 101 , 145 , 67 , 189 , 43 , 88 , 29 , 176 , 58 , 92 , 111 , 134 ,
204+ 78 , 201 , 15 , 84 , 39 , 220 , 66 , 97 , 105 , 142 , 71 , 185 , 49 , 81 , 27 ,
205+ 170 , 53 , 95 , 102 , 147 , 68 , 191 , 44 , 85 , 30 , 177 , 59 , 93 , 112 , 135 ,
206+ 79 , 202 , 16 , 85 , 40 , 221 , 67 , 98 , 23 , 87 , 91 , 65 };
208207 return 1 ;
209208}
210209
@@ -228,8 +227,8 @@ void key::createRSAKey() {
228227 return ;
229228 }
230229
231- auto * keyPublic = new std::vector<uint8_t >();
232- auto * keyPrivate = new std::vector<uint8_t >();
230+ auto * keyPublic = new std::vector<uint8_t >();
231+ auto * keyPrivate = new std::vector<uint8_t >();
233232
234233 createKey (keyPublic, keyPrivate);
235234
@@ -241,14 +240,14 @@ void key::createRSAKey() {
241240 }
242241}
243242
244- std::vector<uint8_t > * key::getPrivateKey (std::string &name) {
245- std::vector<uint8_t >* data = new std::vector<uint8_t >;
243+ std::vector<uint8_t > *key::getPrivateKey (std::string &name) {
244+ std::vector<uint8_t > * data = new std::vector<uint8_t >;
246245 readKey (name, data, false );
247246 return data;
248247}
249248
250- std::vector<uint8_t > * key::getPublicKey (std::string &name) {
251- std::vector<uint8_t >* data = new std::vector<uint8_t >;
249+ std::vector<uint8_t > *key::getPublicKey (std::string &name) {
250+ std::vector<uint8_t > * data = new std::vector<uint8_t >;
252251 readKey (name, data, true );
253252 return data;
254253}
0 commit comments