-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryptor.cpp
More file actions
93 lines (76 loc) · 3.41 KB
/
Copy pathencryptor.cpp
File metadata and controls
93 lines (76 loc) · 3.41 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
/*
* Copyright (C) 2025 Omega493
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <include/pch.hpp>
int main(int argc, char* argv[]) {
cxxopts::Options options("encryptor", "Encrypts or decrypts files");
std::string input_file{};
std::string output_file{};
try {
options.add_options()
("e,encrypt", "File to encrypt", cxxopts::value<std::string>())
("d,decrypt", "File to decrypt", cxxopts::value<std::string>())
("o,output", "Output file (optional)", cxxopts::value<std::string>())
("h,help", "Print usage");
const auto result = options.parse(argc, argv);
if (result.count("h") || argc == 1) {
std::cout << options.help();
return 0;
}
if (result.count("e") && result.count("d")) {
std::cerr << "Error: Cannot use --encrypt (-e) and --decrypt (-d) simultaneously\n" << std::endl;
std::cout << options.help();
return 1;
}
if (result.count("e")) input_file = result["e"].as<std::string>();
else if (result.count("d")) input_file = result["d"].as<std::string>();
else {
std::cerr << "Error: You must specify a mode: --encrypt (-e) or --decrypt (-d)\n" << std::endl;
std::cout << options.help();
return 1;
}
if (sodium_init() < 0) {
std::cerr << "Error: Couldn't initialize libsodium" << std::endl;
return 1;
}
if (result.count("o")) output_file = result["o"].as<std::string>();
else {
const size_t last_dot_pos{ input_file.find_last_of('.') };
std::string base_name{};
if (last_dot_pos == std::string::npos || last_dot_pos == 0) base_name = input_file; // Treat the whole name as the base
else base_name = input_file.substr(0, last_dot_pos); // Get the substring from the start up to the last dot
if (result.count("e")) output_file = base_name + ".enc";
else output_file = base_name + ".dec";
}
if (result.count("e")) encrypt(input_file, output_file);
else if (result.count("d")) decrypt(input_file, output_file);
return 0;
}
catch (const cxxopts::exceptions::exception& e) {
std::cerr << "Error parsing arguments: " << e.what() << std::endl;
std::cout << options.help();
return 1;
}
catch (const UtilException& e) {
std::cerr << "Exception thrown: " << e.what() << "\nThe program will terminate" << std::endl;
return 1;
}
catch (const std::exception& e) {
std::cerr << "Exception thrown: " << e.what() << "\nThe program will now terminate" << std::endl;
return 1;
}
catch (...) {
std::cerr << "Unknown exception thrown\nThe program will now terminate" << std::endl;
return 1;
}
}