diff --git a/GCryptCLI/include/DataFormatter.h b/GCryptCLI/include/DataFormatter.h new file mode 100644 index 0000000..17422ae --- /dev/null +++ b/GCryptCLI/include/DataFormatter.h @@ -0,0 +1,55 @@ +#ifndef GCRYPTCLI_DATAFORMATTER_H +#define GCRYPTCLI_DATAFORMATTER_H + +#include +#include +#include +#include "Configuration.h" + +using namespace Leonetienne::GCrypt; + +// This class has the task to format Blocks to various formats. +class DataFormatter { + public: + //! Will format a single block to a given iobase + static std::string FormatBlock( + const Block& block, + const Configuration::IOBASE_FORMAT base + ); + + //! Will parse a string of a given iobase to a block + static Block DecodeFormat( + const std::string& str, + const Configuration::IOBASE_FORMAT base + ); + + //! Will format a vector of blocks to a given iobase + static std::string FormatBlocks( + const std::vector& blocks, + const Configuration::IOBASE_FORMAT base + ); + + //! Will format a string making up multiple block in a given iobase into a vector of block + static std::vector DecodeFormatMultiblock( + const std::string& str, + const Configuration::IOBASE_FORMAT base + ); + + + private: + static std::string Bin2CustomBase( + const std::string& bin, + const std::vector& customSet, + const std::size_t minLen, + const std::string& seperator = "" + ); + + static std::string CustomBase2Bin( + const std::string& in, + const std::vector& customSet, + const std::string& seperator = "" + ); +}; + +#endif + diff --git a/GCryptCLI/include/ModulePrepareKey.h b/GCryptCLI/include/KeyManager.h similarity index 72% rename from GCryptCLI/include/ModulePrepareKey.h rename to GCryptCLI/include/KeyManager.h index 4f06bc9..7bf1706 100644 --- a/GCryptCLI/include/ModulePrepareKey.h +++ b/GCryptCLI/include/KeyManager.h @@ -1,12 +1,12 @@ -#ifndef GCRYPTCLI_MODULE_PREPAREKEY_H -#define GCRYPTCLI_MODULE_PREPAREKEY_H +#ifndef GCRYPTCLI_KEYMANAGER_H +#define GCRYPTCLI_KEYMANAGER_H #include using namespace Leonetienne::GCrypt; -// This class has the task to supply the encryption key. -class ModulePrepareKey { +// This class has the task to prepare and supply the encryption key. +class KeyManager { public: //! Will prepare the key. Be it from cli, a file, or, random, or whatever. static void PrepareKey(); diff --git a/GCryptCLI/include/ModuleDataFormatter.h b/GCryptCLI/include/ModuleDataFormatter.h deleted file mode 100644 index 5a5b046..0000000 --- a/GCryptCLI/include/ModuleDataFormatter.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef GCRYPTCLI_MODULE_DATAFORMATTER_H -#define GCRYPTCLI_MODULE_DATAFORMATTER_H - -#include -#include -#include -#include "Configuration.h" - -using namespace Leonetienne::GCrypt; - -// This class has the task to format Blocks to various formats. -class ModuleDataFormatter { - public: - //! Will format a single block to a given iobase - static std::string FormatBlock(const Block& block, const Configuration::IOBASE_FORMAT base); - - //! Will parse a string of a given iobase to a block - static Block DecodeFormat(const std::string& str, const Configuration::IOBASE_FORMAT base); - - //! Will format a vector of blocks to a given iobase - static std::string FormatBlocks(const std::vector& blocks, const Configuration::IOBASE_FORMAT base); - - //! Will format a string making up multiple block in a given iobase into a vector of block - static std::vector DecodeFormatMultiblock(const std::string& str, const Configuration::IOBASE_FORMAT base); - - - private: - static std::string Bin2CustomBase(const std::string& bin, const std::vector& customSet, std::size_t minLen, const std::string& seperator = ""); - static std::string CustomBase2Bin(const std::string& in, const std::vector& customSet, const std::string& seperator = ""); -}; - -#endif - diff --git a/GCryptCLI/src/ModuleDataFormatter.cpp b/GCryptCLI/src/DataFormatter.cpp similarity index 92% rename from GCryptCLI/src/ModuleDataFormatter.cpp rename to GCryptCLI/src/DataFormatter.cpp index a2df716..3164a89 100644 --- a/GCryptCLI/src/ModuleDataFormatter.cpp +++ b/GCryptCLI/src/DataFormatter.cpp @@ -1,4 +1,4 @@ -#include "ModuleDataFormatter.h" +#include "DataFormatter.h" #include "Bases.h" #include #include @@ -8,7 +8,7 @@ using namespace Leonetienne::GCrypt; using namespace Leonetienne::StringTools; using namespace Leonetienne::GeneralUtility; -std::string ModuleDataFormatter::FormatBlock( +std::string DataFormatter::FormatBlock( const Block& block, const Configuration::IOBASE_FORMAT base ) { @@ -64,7 +64,7 @@ std::string ModuleDataFormatter::FormatBlock( } } -std::string ModuleDataFormatter::FormatBlocks( +std::string DataFormatter::FormatBlocks( const std::vector& blocks, const Configuration::IOBASE_FORMAT base ) { @@ -89,7 +89,7 @@ std::string ModuleDataFormatter::FormatBlocks( return ss.str(); } -Block ModuleDataFormatter::DecodeFormat( +Block DataFormatter::DecodeFormat( const std::string& str, const Configuration::IOBASE_FORMAT base ) { @@ -163,7 +163,7 @@ Block ModuleDataFormatter::DecodeFormat( return b; } -std::vector ModuleDataFormatter::DecodeFormatMultiblock( +std::vector DataFormatter::DecodeFormatMultiblock( const std::string& str, const Configuration::IOBASE_FORMAT base ) { @@ -238,7 +238,7 @@ std::vector ModuleDataFormatter::DecodeFormatMultiblock( } default: - throw std::invalid_argument("ModuleDataFormatter::StringToBlocks() has been passed an unknown base! No switch-case matched!"); + throw std::invalid_argument("DataFormatter::StringToBlocks() has been passed an unknown base! No switch-case matched!"); break; } @@ -246,7 +246,7 @@ std::vector ModuleDataFormatter::DecodeFormatMultiblock( return blocks; } -std::string ModuleDataFormatter::Bin2CustomBase( +std::string DataFormatter::Bin2CustomBase( const std::string& bin, const std::vector& customSet, const std::size_t minLen, @@ -275,7 +275,7 @@ std::string ModuleDataFormatter::Bin2CustomBase( return ss.str(); } -std::string ModuleDataFormatter::CustomBase2Bin( +std::string DataFormatter::CustomBase2Bin( const std::string& in, const std::vector& customSet, const std::string& seperator @@ -293,7 +293,7 @@ std::string ModuleDataFormatter::CustomBase2Bin( ); if (binary.length() != Block::BLOCK_SIZE_BITS) { - throw std::invalid_argument("ModuleDataFormatter::CustomBase2Bin() received input that doesn't translate to a bitstring of length 512!"); + throw std::invalid_argument("DataFormatter::CustomBase2Bin() received input that doesn't translate to a bitstring of length 512!"); } return binary; diff --git a/GCryptCLI/src/ModulePrepareKey.cpp b/GCryptCLI/src/KeyManager.cpp similarity index 92% rename from GCryptCLI/src/ModulePrepareKey.cpp rename to GCryptCLI/src/KeyManager.cpp index be7c60f..a904692 100644 --- a/GCryptCLI/src/ModulePrepareKey.cpp +++ b/GCryptCLI/src/KeyManager.cpp @@ -1,4 +1,4 @@ -#include "ModulePrepareKey.h" +#include "KeyManager.h" #include "CommandlineInterface.h" #include "Configuration.h" #include @@ -12,7 +12,7 @@ #include #endif -void ModulePrepareKey::PrepareKey() { +void KeyManager::PrepareKey() { // Special-case: We are hashing: // no key needed. @@ -67,11 +67,11 @@ void ModulePrepareKey::PrepareKey() { } -const Key& ModulePrepareKey::GetKey() { +const Key& KeyManager::GetKey() { return key; } -std::string ModulePrepareKey::PasswordPrompt() { +std::string KeyManager::PasswordPrompt() { // Disable stdin-echo #if defined _WIN32 || defined _WIN64 HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); @@ -103,5 +103,5 @@ std::string ModulePrepareKey::PasswordPrompt() { return password; } -Key ModulePrepareKey::key; +Key KeyManager::key; diff --git a/GCryptCLI/src/main.cpp b/GCryptCLI/src/main.cpp index ed38a45..af8f449 100644 --- a/GCryptCLI/src/main.cpp +++ b/GCryptCLI/src/main.cpp @@ -1,6 +1,6 @@ #include "CommandlineInterface.h" #include "Configuration.h" -#include "ModulePrepareKey.h" +#include "KeyManager.h" int main(int argc, char* const* argv) { @@ -11,7 +11,7 @@ int main(int argc, char* const* argv) { Configuration::Parse(); // Prepare the key - ModulePrepareKey::PrepareKey(); + KeyManager::PrepareKey(); return 0; } diff --git a/GCryptCLI/test/DataFormatter.cpp b/GCryptCLI/test/DataFormatter.cpp index 1e6317c..186c5d8 100644 --- a/GCryptCLI/test/DataFormatter.cpp +++ b/GCryptCLI/test/DataFormatter.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include "Catch2.h" @@ -8,7 +8,7 @@ using namespace Leonetienne::GCrypt; // Tests that recoding iobase formats works for individual blocks, with random data TEMPLATE_TEST_CASE_SIG( __FILE__"Data formats can be converted, without changing in value, with indivudual blocks, with random data", - "[ModuleDataFormatter]", + "[DataFormatter]", ((Configuration::IOBASE_FORMAT formatUnderTest), formatUnderTest), Configuration::IOBASE_FORMAT::BASE_BYTES, Configuration::IOBASE_FORMAT::BASE_2, @@ -34,13 +34,13 @@ TEMPLATE_TEST_CASE_SIG( // Exercise // Convert to a custom base - const std::string b_format = ModuleDataFormatter::FormatBlock( + const std::string b_format = DataFormatter::FormatBlock( b_initial, formatUnderTest ); // Convert back to a block - const Block b_retrieved = ModuleDataFormatter::DecodeFormat( + const Block b_retrieved = DataFormatter::DecodeFormat( b_format, formatUnderTest ); @@ -54,7 +54,7 @@ TEMPLATE_TEST_CASE_SIG( // Tests that recoding iobase format works TEMPLATE_TEST_CASE_SIG( __FILE__"Data formats can be converted, without changing in value, with indivudual blocks, with very little data (lots of nullbytes)", - "[ModuleDataFormatter]", + "[DataFormatter]", ((Configuration::IOBASE_FORMAT formatUnderTest), formatUnderTest), Configuration::IOBASE_FORMAT::BASE_BYTES, Configuration::IOBASE_FORMAT::BASE_2, @@ -72,13 +72,13 @@ TEMPLATE_TEST_CASE_SIG( // Exercise // Convert to a custom base - const std::string b_format = ModuleDataFormatter::FormatBlock( + const std::string b_format = DataFormatter::FormatBlock( b_initial, formatUnderTest ); // Convert back to a block - const Block b_retrieved = ModuleDataFormatter::DecodeFormat( + const Block b_retrieved = DataFormatter::DecodeFormat( b_format, formatUnderTest );