Skip to content

Commit e63b5c9

Browse files
Format project (#11)
Co-authored-by: Andreas Böhm <134922046+LordofGhost@users.noreply.github.com>
1 parent 3091e5f commit e63b5c9

10 files changed

Lines changed: 378 additions & 427 deletions

File tree

src/cli.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1+
#include "cli.h"
2+
13
#include <iostream>
24

3-
#include "cli.h"
45
#include "key.h"
56

6-
void cli::help() {
7-
std::cout << "Welcome to RSA-Encryptor" << std::endl;
8-
}
7+
void cli::help() { std::cout << "Welcome to RSA-Encryptor" << std::endl; }
98

10-
void cli::keyManager::create() {
11-
key::createRSAKey();
12-
}
9+
void cli::keyManager::create() { key::createRSAKey(); }
1310

14-
void cli::keyManager::list() {
15-
}
11+
void cli::keyManager::list() {}
1612

17-
void cli::keyManager::print(const std::string& name, bool publicKey, bool privateKey) {
18-
}
13+
void cli::keyManager::print(const std::string& name, bool publicKey, bool privateKey) {}

src/cli.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#ifndef CLI_H
22
#define CLI_H
33

4+
#include <string>
5+
46
namespace cli {
5-
void help();
6-
7-
namespace keyManager {
8-
void create();
9-
void list();
10-
void print(const std::string& name, bool publicKey, bool privateKey);
11-
}
12-
}
7+
void help();
8+
9+
namespace keyManager {
10+
void create();
11+
void list();
12+
void print(const std::string& name, bool publicKey, bool privateKey);
13+
} // namespace keyManager
14+
} // namespace cli
1315

1416
#endif

src/encryption.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#include <cmath>
2-
31
#include "encryption.h"
42

3+
#include <cmath>
4+
55
std::int8_t encrypt(std::uint8_t data, unsigned long int e, unsigned long N) {
66
return std::pow(data, e);
77
}

src/encryption.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
#ifndef ENCRYPTION_H
22
#define ENCRYPTION_H
33

4-
#include <iostream>
54
#include <cstdint>
5+
#include <iostream>
66

7-
class encryption
8-
{
9-
private:
10-
public:
7+
class encryption {
8+
private:
9+
public:
1110
std::int8_t encrypt(std::uint8_t data, unsigned long int e, unsigned long N);
1211
};
1312

src/key.cpp

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
#include "key.h"
2+
13
#include <filesystem>
24
#include <fstream>
35
#include <string>
46

5-
#include "key.h"
6-
77
std::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

1414
int 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
7080
int 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) {
143151
std::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

189198
int 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
}

src/key.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
#ifndef KEY_H
22
#define KEY_H
33

4-
#include <iostream>
54
#include <filesystem>
5+
#include <iostream>
66
#include <map>
77
#include <vector>
88

99
#define KEY_FOLDER "rsa-keys"
1010

11-
enum {
12-
NONE,
13-
PUBLIC,
14-
PRIVATE,
15-
BOTH
16-
};
11+
enum { NONE, PUBLIC, PRIVATE, BOTH };
1712

1813
const char base64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1914

2015
class key {
21-
private:
16+
private:
2217
static std::filesystem::path keysPath();
2318
static int keyExists(std::string name);
2419
static void generatePublicFromPrivate();
@@ -33,11 +28,11 @@ class key {
3328

3429
static int createKey(std::vector<uint8_t> *keyPublic, std::vector<uint8_t> *keyPrivate);
3530

36-
public:
31+
public:
3732
static void createRSAKey();
3833

39-
static std::vector<uint8_t>* getPrivateKey(std::string &name);
40-
static std::vector<uint8_t>* getPublicKey(std::string &name);
34+
static std::vector<uint8_t> *getPrivateKey(std::string &name);
35+
static std::vector<uint8_t> *getPublicKey(std::string &name);
4136
};
4237

4338
#endif

0 commit comments

Comments
 (0)