Got block conversions working...
This commit is contained in:
parent
f6d646da55
commit
8e04e91e88
@ -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<Block>& blocks, 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 Bin2CustomBase(const std::string& bin, const std::vector<std::string>& customSet, std::size_t minLen, const std::string& seperator = "");
|
||||
static std::string CustomBase2Bin(const std::string& in, const std::vector<std::string>& customSet, const std::string& seperator = "");
|
||||
};
|
||||
|
||||
|
@ -3,11 +3,41 @@
|
||||
#include <GeneralUtility/BaseConversion.h>
|
||||
#include <StringTools/StringTools.h>
|
||||
#include <GCrypt/Util.h>
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
|
||||
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<Configuration::IOBASE_FORMAT, std::size_t>({
|
||||
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<Block>& 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<std::string>& customSet,
|
||||
const std::size_t minLen,
|
||||
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);
|
||||
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::string, std::vector<std::string>>(
|
||||
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::vector<std::string>, std::string>(in_symbols, customSet, std::string("01"));
|
||||
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::vector<std::string>, 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;
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "Configuration.h"
|
||||
#include "ModulePrepareKey.h"
|
||||
#include "ModuleDataFormatter.h"
|
||||
#include "Bases.h"
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user