-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.cpp
More file actions
127 lines (91 loc) · 3.16 KB
/
encrypt.cpp
File metadata and controls
127 lines (91 loc) · 3.16 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
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
#include <filesystem>
#include <string>
#include <fstream>
#include <iostream>
#include <sodium.h>
#include <sodium/core.h>
#include <sodium/crypto_box.h>
#include <sodium/crypto_secretstream_xchacha20poly1305.h>
#include "headers.h"
int encrypt(Config cfg) {
namespace fs = std::filesystem;
if(cfg.pubDir.length() > 0 && cfg.secDir.length() > 0) {
std::cout << "Keys found.\n";
} else {
if(!fs::is_directory(cfg.keysDir)) {
std::cout << "Generating keys...\n";
generateKeypair();
}
}
if(sodium_init() != 0) {
std::cerr << "Error sodium\n";
return -1;
}
std::string inputfile = cfg.file;
if(cfg.isDir) {
tarArchive(cfg.file);
inputfile = "archive.tar";
}
std::ifstream file(inputfile, std::ios::binary);
if(!file) {
std::cerr << "Cannot open input file\n";
return -1;
}
// Reading keys
unsigned char publicKey[crypto_box_PUBLICKEYBYTES];
unsigned char secretKey[crypto_box_SECRETKEYBYTES];
fs::path pubPath, secPath;
findKeys(pubPath, secPath, cfg);
std::ifstream pubFile(pubPath, std::ios::binary);
std::ifstream secFile(secPath, std::ios::binary);
pubFile.read(reinterpret_cast<char*>(publicKey), crypto_box_PUBLICKEYBYTES);
secFile.read(reinterpret_cast<char*>(secretKey), crypto_box_SECRETKEYBYTES);
// Generating stream key
unsigned char streamKey[crypto_secretstream_xchacha20poly1305_KEYBYTES];
crypto_secretstream_xchacha20poly1305_keygen(streamKey);
unsigned char boxNonce[crypto_box_NONCEBYTES];
randombytes_buf(boxNonce, sizeof boxNonce);
unsigned char boxedKey[crypto_box_MACBYTES + crypto_secretstream_xchacha20poly1305_KEYBYTES];
if(crypto_box_easy(boxedKey, streamKey, sizeof streamKey, boxNonce, publicKey, secretKey) != 0) {
std::cerr << "Failed to encrypt.\n";
return 1;
}
crypto_secretstream_xchacha20poly1305_state state;
unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
crypto_secretstream_xchacha20poly1305_init_push(&state, header, streamKey);
// Writing official data into file
std::string filename;
if(cfg.filename != "" && cfg.filename.size() > 0) {
filename = cfg.filename;
} else {
filename = cfg.file + ".enc";
}
std::ofstream out(filename.c_str(), std::ios::binary);
out.write(reinterpret_cast<char*>(boxNonce), sizeof boxNonce);
out.write(reinterpret_cast<char*>(boxedKey), sizeof boxedKey);
out.write(reinterpret_cast<char*>(header), sizeof header);
// Encypting file by chunks
unsigned char fileBuffer[CHUNK_SIZE];
unsigned char outBuffer[CHUNK_SIZE + crypto_secretstream_xchacha20poly1305_ABYTES];
while(file.good()) {
file.read(reinterpret_cast<char*>(fileBuffer), CHUNK_SIZE);
std::streamsize readBytes = file.gcount();
if(readBytes <= 0) break;
unsigned long long out_len;
crypto_secretstream_xchacha20poly1305_push(
&state,
outBuffer,
&out_len,
fileBuffer,
readBytes,
nullptr, 0,
crypto_secretstream_xchacha20poly1305_TAG_MESSAGE
);
out.write(reinterpret_cast<char*>(outBuffer), out_len);
}
if(cfg.isDir) {
fs::remove("archive.tar");
}
std::cout << "Encrypted successfully\n";
return 0;
}