From 8e04e91e88f8c58f88e499112acedd5c97e1ced4 Mon Sep 17 00:00:00 2001 From: Leonetienne Date: Fri, 27 May 2022 02:13:05 +0200 Subject: [PATCH] Got block conversions working... --- GCryptCLI/include/ModuleDataFormatter.h | 8 ++- GCryptCLI/src/ModuleDataFormatter.cpp | 71 +++++++++++++++++++++---- GCryptCLI/src/main.cpp | 65 +++++++++++++++++++++- 3 files changed, 133 insertions(+), 11 deletions(-) diff --git a/GCryptCLI/include/ModuleDataFormatter.h b/GCryptCLI/include/ModuleDataFormatter.h index f08276a..6936158 100644 --- a/GCryptCLI/include/ModuleDataFormatter.h +++ b/GCryptCLI/include/ModuleDataFormatter.h @@ -11,12 +11,18 @@ 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 StringToBlock(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); + private: - static std::string Bin2CustomBase(const std::string& bin, const std::vector& customSet, const std::string& seperator = ""); + 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 = ""); }; diff --git a/GCryptCLI/src/ModuleDataFormatter.cpp b/GCryptCLI/src/ModuleDataFormatter.cpp index eca1014..583defb 100644 --- a/GCryptCLI/src/ModuleDataFormatter.cpp +++ b/GCryptCLI/src/ModuleDataFormatter.cpp @@ -3,11 +3,41 @@ #include #include #include +#include +#include using namespace Leonetienne::GCrypt; using namespace Leonetienne::StringTools; using namespace Leonetienne::GeneralUtility; +namespace { + // This lookup table holds how many digits a block is long + // in any iobase. + // This cannot be calculated on the fly, as it involves + // arithmetic with involving REALLY big numbers (like, 2^512). + // Here's how to calculate these numbers: floor(log_setLen(2^512)). + // So, if our set would consist of 10 digits, it would be: + // floor(log10(2^512)) = 154. + auto blockLengthByBase = + std::map({ + std::make_pair(Configuration::IOBASE_FORMAT::BASE_BYTES, 64), + std::make_pair(Configuration::IOBASE_FORMAT::BASE_2, 512), + std::make_pair(Configuration::IOBASE_FORMAT::BASE_8, 171), + std::make_pair(Configuration::IOBASE_FORMAT::BASE_10, 155), + std::make_pair(Configuration::IOBASE_FORMAT::BASE_16, 128), + std::make_pair(Configuration::IOBASE_FORMAT::BASE_UWU, 125), + std::make_pair(Configuration::IOBASE_FORMAT::BASE_UGH, 125) + }); +} + +std::string ModuleDataFormatter::FormatBlocks( + const std::vector& blocks, + const Configuration::IOBASE_FORMAT base +) { + + + return ""; +} std::string ModuleDataFormatter::FormatBlock( const Block& block, @@ -23,13 +53,15 @@ std::string ModuleDataFormatter::FormatBlock( case Configuration::IOBASE_FORMAT::BASE_8: return Bin2CustomBase( block.ToBinaryString(), - BASE_8 + BASE_8, + blockLengthByBase[Configuration::IOBASE_FORMAT::BASE_8] ); case Configuration::IOBASE_FORMAT::BASE_10: return Bin2CustomBase( block.ToBinaryString(), - BASE_10 + BASE_10, + blockLengthByBase[Configuration::IOBASE_FORMAT::BASE_10] ); case Configuration::IOBASE_FORMAT::BASE_16: @@ -38,13 +70,15 @@ std::string ModuleDataFormatter::FormatBlock( case Configuration::IOBASE_FORMAT::BASE_64: return Bin2CustomBase( block.ToBinaryString(), - BASE_64 + BASE_64, + blockLengthByBase[Configuration::IOBASE_FORMAT::BASE_64] ); case Configuration::IOBASE_FORMAT::BASE_UWU: return Bin2CustomBase( block.ToBinaryString(), BASE_UWU, + blockLengthByBase[Configuration::IOBASE_FORMAT::BASE_UWU], " " ); @@ -52,6 +86,7 @@ std::string ModuleDataFormatter::FormatBlock( return Bin2CustomBase( block.ToBinaryString(), BASE_UGH, + blockLengthByBase[Configuration::IOBASE_FORMAT::BASE_UGH], " " ); @@ -134,17 +169,22 @@ Block ModuleDataFormatter::StringToBlock( return b; } - std::string ModuleDataFormatter::Bin2CustomBase( const std::string& bin, const std::vector& customSet, + const std::size_t minLen, const std::string& seperator ) { std::stringstream ss; // Translate to custom set const std::vector vec_base_custom = - Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y>(bin, "01", customSet); + Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y>( + bin, + "01", + customSet, + minLen + ); // Convert to string for (std::size_t i = 0; i < vec_base_custom.size(); i++) { @@ -168,14 +208,27 @@ std::string ModuleDataFormatter::CustomBase2Bin( // Translate to binary std::string binary = - Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y, std::string>(in_symbols, customSet, std::string("01")); + Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y, std::string>( + in_symbols, + customSet, + std::string("01"), + Block::BLOCK_SIZE_BITS + ); // Pad to BLOCK_SIZE - binary = PadStringToLength(binary, Block::BLOCK_SIZE_BITS, '0', false); + //binary = PadStringToLength(binary, Block::BLOCK_SIZE_BITS, '0', false); + + // Because a set may not perfectly fit a block, transcoding it back + // to binary may yield more than 512 bit. These other bits could never + // be 1. We have to trim them. + std::cout << binary << std::endl << std::endl; + if (binary.length() > Block::BLOCK_SIZE_BITS) { + binary = binary.substr(0, Block::BLOCK_SIZE_BITS); + } - // Check that our string is of size BLOCK_SIZE if (binary.length() != Block::BLOCK_SIZE_BITS) { - throw std::invalid_argument("ModuleDataFormatter::CustomBase2Bin received input, which does note translate to a bitstring of size BLOCK_SIZE. Huh?"); + std::cout << binary.length() << std::endl; + throw std::invalid_argument("ModuleDataFormatter::CustomBase2Bin() received input that doesn't translate to a bitstring of length 512!"); } return binary; diff --git a/GCryptCLI/src/main.cpp b/GCryptCLI/src/main.cpp index 9683abd..ca29aac 100644 --- a/GCryptCLI/src/main.cpp +++ b/GCryptCLI/src/main.cpp @@ -2,6 +2,7 @@ #include "Configuration.h" #include "ModulePrepareKey.h" #include "ModuleDataFormatter.h" +#include "Bases.h" #include int main(int argc, char* const* argv) { @@ -15,11 +16,73 @@ int main(int argc, char* const* argv) { // Prepare the key ModulePrepareKey::PrepareKey(); + Block block; + block.FromTextString("Hello World :3"); + std::cout << block.ToBinaryString() << std::endl << std::endl; + std::cout << block.ToHexString() << std::endl << std::endl; + + + /* + Block all1; + for (std::size_t i = 0; i < 16; i++) + all1[i] = -1; + std::cout << ModuleDataFormatter::FormatBlock( - ModulePrepareKey::GetKey(), + all1, Configuration::iobaseFormat ) + << std::endl + << std::endl; + + std::cout + << ModuleDataFormatter::FormatBlock( + all1, + Configuration::iobaseFormat + ).size() + << std::endl + << std::endl; + */ + + /* + std::cout + << ModuleDataFormatter::FormatBlock( + //ModulePrepareKey::GetKey(), + block, + Configuration::iobaseFormat + ) + << std::endl + << std::endl; + */ + + + /* + const std::string customBase = + ModuleDataFormatter::FormatBlock( + block, + Configuration::iobaseFormat + ); + + std::cout << "\"" << customBase << "\"" << std::endl << std::endl; + + const Block back = + ModuleDataFormatter::StringToBlock( + customBase, + Configuration::iobaseFormat + ); + + std::cout << back.ToHexString() << std::endl << std::endl; + */ + + std::cout + << ModuleDataFormatter::StringToBlock( + ModuleDataFormatter::FormatBlock( + //ModulePrepareKey::GetKey(), + block, + Configuration::iobaseFormat + ), + Configuration::iobaseFormat + ).ToTextString() << std::endl; return 0;