-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.cpp
More file actions
82 lines (60 loc) · 2.15 KB
/
misc.cpp
File metadata and controls
82 lines (60 loc) · 2.15 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
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sodium/crypto_box.h>
#include <sodium/crypto_generichash.h>
#include <string>
#include <sodium.h>
#include <filesystem>
#include "headers.h"
void generateKeypair() {
std::filesystem::create_directories("keys/publicKey");
std::filesystem::create_directories("keys/secretKey");
unsigned char publicKey[crypto_box_PUBLICKEYBYTES];
unsigned char secretKey[crypto_box_SECRETKEYBYTES];
crypto_box_keypair(publicKey, secretKey);
unsigned char hash[crypto_generichash_BYTES];
crypto_generichash(hash, sizeof(hash), publicKey, crypto_box_PUBLICKEYBYTES, nullptr, 0);
char hex[crypto_generichash_BYTES * 2 + 1];
sodium_bin2hex(hex, sizeof(hex), hash, sizeof(hash));
std::string shortHex = std::string(hex).substr(0, 6);
std::string publicName = shortHex + ".pub";
std::ofstream pk(("keys/publicKey/" + publicName).c_str(), std::ios::binary);
std::ofstream sc(("keys/secretKey/" + shortHex).c_str(), std::ios::binary);
pk.write(reinterpret_cast<const char*>(publicKey), crypto_box_PUBLICKEYBYTES);
sc.write(reinterpret_cast<const char*>(secretKey), crypto_box_SECRETKEYBYTES);
pk.close();
sc.close();
}
bool tarArchive(const std::string& dir) {
if(system("tar --version > /dev/null 2>&1") != 0) {
std::cerr << "Tar not found.\n";
return false;
}
std::string cmd = "tar -cf archive.tar --numeric-owner --owner=0 --group=0"
" --mtime='1970-01-01' --sort=name " + dir;
return system(cmd.c_str()) == 0;
}
void findKeys(fs::path& pubPath, fs::path&secPath, Config cfg){
if(cfg.pubDir.length() > 1) {
pubPath = cfg.pubDir;
}
if(cfg.secDir.length() > 1) {
secPath = cfg.secDir;
}
if(cfg.pubDir.empty() || cfg.secDir.empty()) {
for (const auto& entry : fs::recursive_directory_iterator(cfg.keysDir)) {
const auto& path = entry.path();
if(cfg.pubDir.length() < 1) {
if(entry.path().extension() == ".pub") {
pubPath = entry.path();
}
}
if(cfg.secDir.length() < 1) {
if(!path.has_extension() && entry.is_regular_file()) {
secPath = entry.path();
}
}
}
}
}