Implement output formatting module
This commit is contained in:
parent
fad87e9944
commit
6ac775105b
23
GCryptCLI/include/ModuleDataFormatter.h
Normal file
23
GCryptCLI/include/ModuleDataFormatter.h
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef GCRYPTCLI_MODULE_DATAFORMATTER_H
|
||||
#define GCRYPTCLI_MODULE_DATAFORMATTER_H
|
||||
|
||||
#include <GCrypt/Block.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "Configuration.h"
|
||||
|
||||
using namespace Leonetienne::GCrypt;
|
||||
|
||||
// This class has the task to format Blocks to various formats.
|
||||
class ModuleDataFormatter {
|
||||
public:
|
||||
static std::string FormatBlock(const Block& block, const Configuration::IOBASE_FORMAT base);
|
||||
|
||||
|
||||
private:
|
||||
static std::string Bin2CustomBase(const std::string& bin, const std::vector<std::string>& customSet, const std::string& seperator = "");
|
||||
static std::string CustomBase2Bin(const std::string& in, const std::vector<std::string>& customSet, const std::string& seperator = "");
|
||||
};
|
||||
|
||||
#endif
|
||||
|
104
GCryptCLI/src/ModuleDataFormatter.cpp
Normal file
104
GCryptCLI/src/ModuleDataFormatter.cpp
Normal file
@ -0,0 +1,104 @@
|
||||
#include "ModuleDataFormatter.h"
|
||||
#include "Bases.h"
|
||||
#include <GeneralUtility/BaseConversion.h>
|
||||
#include <StringTools/StringTools.h>
|
||||
|
||||
using namespace Leonetienne::GCrypt;
|
||||
using namespace Leonetienne::StringTools;
|
||||
using namespace Leonetienne::GeneralUtility;
|
||||
|
||||
|
||||
std::string ModuleDataFormatter::FormatBlock(
|
||||
const Block& block,
|
||||
const Configuration::IOBASE_FORMAT base
|
||||
) {
|
||||
switch (base) {
|
||||
case Configuration::IOBASE_FORMAT::BASE_BYTES:
|
||||
return block.ToByteString();
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_2:
|
||||
return block.ToBinaryString();
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_8:
|
||||
return Bin2CustomBase(
|
||||
block.ToBinaryString(),
|
||||
BASE_8
|
||||
);
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_10:
|
||||
return Bin2CustomBase(
|
||||
block.ToBinaryString(),
|
||||
BASE_10
|
||||
);
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_16:
|
||||
return block.ToHexString();
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_64:
|
||||
return Bin2CustomBase(
|
||||
block.ToBinaryString(),
|
||||
BASE_64
|
||||
);
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_UWU:
|
||||
return Bin2CustomBase(
|
||||
block.ToBinaryString(),
|
||||
BASE_UWU,
|
||||
" "
|
||||
);
|
||||
|
||||
case Configuration::IOBASE_FORMAT::BASE_UGH:
|
||||
return Bin2CustomBase(
|
||||
block.ToBinaryString(),
|
||||
BASE_UGH,
|
||||
" "
|
||||
);
|
||||
|
||||
default:
|
||||
throw std::invalid_argument("Iobase now found! Oh no. Anyway.");
|
||||
}
|
||||
}
|
||||
|
||||
std::string ModuleDataFormatter::Bin2CustomBase(
|
||||
const std::string& bin,
|
||||
const std::vector<std::string>& customSet,
|
||||
const std::string& seperator
|
||||
) {
|
||||
std::stringstream ss;
|
||||
|
||||
// Translate to custom set
|
||||
const std::vector<std::string> vec_base_custom =
|
||||
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::string, std::vector<std::string>>(bin, "01", customSet);
|
||||
|
||||
// Convert to string
|
||||
for (std::size_t i = 0; i < vec_base_custom.size(); i++) {
|
||||
ss << vec_base_custom[i];
|
||||
|
||||
if (i != vec_base_custom.size() - 1) {
|
||||
ss << seperator;
|
||||
}
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string ModuleDataFormatter::CustomBase2Bin(
|
||||
const std::string& in,
|
||||
const std::vector<std::string>& customSet,
|
||||
const std::string& seperator
|
||||
) {
|
||||
// Split input into symbols
|
||||
const std::vector<std::string> in_symbols = StringTools::Split(in, seperator);
|
||||
|
||||
// Translate to binary
|
||||
std::string binary =
|
||||
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::vector<std::string>, std::string>(in_symbols, customSet, std::string("01"));
|
||||
|
||||
// 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?");
|
||||
}
|
||||
|
||||
return binary;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "CommandlineInterface.h"
|
||||
#include "Configuration.h"
|
||||
#include "ModulePrepareKey.h"
|
||||
#include "ModuleDataFormatter.h"
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char* const* argv) {
|
||||
@ -14,7 +15,12 @@ int main(int argc, char* const* argv) {
|
||||
// Prepare the key
|
||||
ModulePrepareKey::PrepareKey();
|
||||
|
||||
std::cout << ModulePrepareKey::GetKey().ToHexString() << std::endl;
|
||||
std::cout
|
||||
<< ModuleDataFormatter::FormatBlock(
|
||||
ModulePrepareKey::GetKey(),
|
||||
Configuration::iobaseFormat
|
||||
)
|
||||
<< std::endl;
|
||||
|
||||
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user