11// Privacy Shield: A Suite of Tools Designed to Facilitate Privacy Management.
2- // Copyright (C) 2024 Ian Duncan <dr8co@duck.com>
2+ // Copyright (C) 2025 Ian Duncan <dr8co@duck.com>
33//
44// This program is free software: you can redistribute it and/or modify
55// it under the terms of the GNU General Public License as published by
1414// You should have received a copy of the GNU General Public License
1515// along with this program. If not, see https://www.gnu.org/licenses.
1616
17- module ;
1817
1918#include < algorithm>
2019#include < system_error>
2120#include < utility>
22- #include < format>
2321#include < cmath>
24- #include < unordered_map>
25- #include < filesystem>
2622#include < gcrypt.h>
2723#include < sodium.h>
28- #include < print>
2924
30- import utils;
31- import secureAllocator;
32- import mimallocSTL;
33- import passwordManager;
34-
35- module encryption;
25+ #include " encryption.hpp"
26+ #include " ../passwordManager/passwordManager.hpp"
27+ #include " ../utils/utils.hpp"
3628
3729namespace fs = std::filesystem;
3830
31+ // clang-format off
32+
3933// / \brief Available encryption/decryption ciphers.
4034enum class Algorithms : std::uint_fast8_t {
4135 AES = 1 << 0 ,
@@ -60,17 +54,18 @@ constexpr struct {
6054 const gcry_cipher_algos Twofish = GCRY_CIPHER_TWOFISH;
6155} AlgoSelection;
6256
57+ // clang-format on
58+
6359
6460// / \brief Formats a file size into a human-readable string.
6561// / \param size The file size as an unsigned integer.
6662// / \return A string representing the formatted file size.
67- miSTL::string formatFileSize (const std::uintmax_t & size) {
63+ miSTL::string formatFileSize (const std::uintmax_t & size) {
6864 int i{};
6965 auto mantissa = static_cast <double >(size);
70- for (; mantissa >= 1024 .; mantissa /= 1024 ., ++i) {
71- }
66+ for (; mantissa >= 1024 .; mantissa /= 1024 ., ++i) {}
7267 mantissa = std::ceil (mantissa * 10 .) / 10 .;
73- miSTL::string result { std::to_string (mantissa) + " BKMGTPE" [i]};
68+ miSTL::string result{ std::to_string (mantissa) + " BKMGTPE" [i]};
7469 return i == 0 ? result : result + " B (" + std::to_string (size).c_str () + ' )' ;
7570}
7671
@@ -80,7 +75,7 @@ miSTL::string formatFileSize(const std::uintmax_t &size) {
8075// / \throws std::invalid_argument if \p mode is invalid.
8176// / \throws std::runtime_error if the input file does not exist, is a directory,
8277// / is not a regular file, or is not readable.
83- void checkInputFile (const fs::path & inFile, const OperationMode & mode) {
78+ void checkInputFile (const fs::path& inFile, const OperationMode& mode) {
8479 if (mode != OperationMode::Encryption && mode != OperationMode::Decryption)
8580 throw std::invalid_argument (" Invalid mode of operation." );
8681
@@ -109,7 +104,7 @@ void checkInputFile(const fs::path &inFile, const OperationMode &mode) {
109104// / \brief Creates non-existing parent directories for a file.
110105// / \param filePath The file path for which the directory path needs to be created.
111106// / \return True if the directory path is created successfully or already exists, false otherwise.
112- bool createPath (const fs::path & filePath) noexcept {
107+ bool createPath (const fs::path& filePath) noexcept {
113108 if (filePath.string ().empty ()) return false ; // Can't create empty paths
114109
115110 std::error_code ec;
@@ -136,7 +131,7 @@ bool createPath(const fs::path &filePath) noexcept {
136131// / \param mode the mode of operation: encryption or decryption.
137132// / \throws std::invalid_argument if \p mode is invalid.
138133// / \throws std::runtime_error if the output file is not writable, readable, or there is not enough space to save it.
139- inline void checkOutputFile (const fs::path & inFile, fs::path & outFile, const OperationMode & mode) {
134+ inline void checkOutputFile (const fs::path& inFile, fs::path& outFile, const OperationMode& mode) {
140135 if (mode != OperationMode::Encryption && mode != OperationMode::Decryption)
141136 throw std::invalid_argument (" Invalid mode of operation." );
142137
@@ -212,8 +207,8 @@ inline void copyLastWrite(const std::string_view srcFile, const std::string_view
212207// / \param password the password to use for encryption/decryption.
213208// / \param algo the algorithm to use for encryption/decryption.
214209// / \param mode the mode of operation: encryption or decryption.
215- void fileEncryptionDecryption (const miSTL::string & inputFileName, const miSTL::string & outputFileName,
216- const privacy::string & password, const Algorithms & algo, const OperationMode & mode) {
210+ void fileEncryptionDecryption (const miSTL::string& inputFileName, const miSTL::string& outputFileName,
211+ const privacy::string& password, const Algorithms& algo, const OperationMode& mode) {
217212 // The mode must be valid: must be either encryption or decryption
218213 if (mode != OperationMode::Encryption && mode != OperationMode::Decryption) [[unlikely]] {
219214 printColoredErrorln (' r' , " Invalid mode of operation." );
@@ -222,15 +217,15 @@ void fileEncryptionDecryption(const miSTL::string &inputFileName, const miSTL::s
222217
223218 try {
224219 // / Encrypts/decrypts a file based on the passed mode and algorithm.
225- auto encryptDecrypt = [&](const miSTL::string & algorithm) -> void {
220+ auto encryptDecrypt = [&](const miSTL::string& algorithm) -> void {
226221 if (mode == OperationMode::Encryption) // Encryption
227222 encryptFile (inputFileName, outputFileName, password, algorithm);
228223 else // Decryption
229224 decryptFile (inputFileName, outputFileName, password, algorithm);
230225 };
231226
232227 // / Encrypts/decrypts a file using a cipher with more rounds.
233- auto encryptDecryptMoreRounds = [&](const gcry_cipher_algos & algorithm) -> void {
228+ auto encryptDecryptMoreRounds = [&](const gcry_cipher_algos& algorithm) -> void {
234229 if (mode == OperationMode::Encryption) // Encryption
235230 encryptFileWithMoreRounds (inputFileName, outputFileName, password, algorithm);
236231 else // Decryption
@@ -264,11 +259,11 @@ void fileEncryptionDecryption(const miSTL::string &inputFileName, const miSTL::s
264259 // Preserve file permissions
265260 if (!copyFilePermissions (inputFileName, outputFileName))
266261 [[unlikely]]
267- printColoredOutputln (' m' , " Check the permissions of the {}crypted file." , pre );
262+ printColoredOutputln (' m' , " Check the permissions of the {}crypted file." , pre );
268263
269264 // Try to preserve the time of last modification
270265 copyLastWrite (inputFileName, outputFileName);
271- } catch (const std::exception & ex) {
266+ } catch (const std::exception& ex) {
272267 printColoredErrorln (' r' , " Error: {}" , ex.what ());
273268 }
274269}
@@ -371,10 +366,11 @@ void encryptDecrypt() {
371366 printColoredOutput (' c' , " {}" , algoDescription.find (cipher)->second );
372367 printColoredOutputln (' g' , " ..." );
373368
374- fileEncryptionDecryption (canonical (inputPath).string ().c_str (), weakly_canonical (outputPath).string ().c_str (),
369+ fileEncryptionDecryption (canonical (inputPath).string ().c_str (),
370+ weakly_canonical (outputPath).string ().c_str (),
375371 password, cipher, static_cast <OperationMode>(choice));
376372 std::println (" " );
377- } catch (const std::exception & ex) {
373+ } catch (const std::exception& ex) {
378374 printColoredError (' y' , " Error: " );
379375 printColoredErrorln (' r' , " {}" , ex.what ());
380376 }
0 commit comments