A bit of cleanup

This commit is contained in:
Leonetienne 2022-05-27 16:44:42 +02:00
parent b0d0f831d9
commit 5365233b43
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
7 changed files with 82 additions and 60 deletions

View File

@ -0,0 +1,55 @@
#ifndef GCRYPTCLI_DATAFORMATTER_H
#define GCRYPTCLI_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 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<Block>& 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<Block> DecodeFormatMultiblock(
const std::string& str,
const Configuration::IOBASE_FORMAT base
);
private:
static std::string Bin2CustomBase(
const std::string& bin,
const std::vector<std::string>& customSet,
const 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 = ""
);
};
#endif

View File

@ -1,12 +1,12 @@
#ifndef GCRYPTCLI_MODULE_PREPAREKEY_H
#define GCRYPTCLI_MODULE_PREPAREKEY_H
#ifndef GCRYPTCLI_KEYMANAGER_H
#define GCRYPTCLI_KEYMANAGER_H
#include <GCrypt/Key.h>
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();

View File

@ -1,33 +0,0 @@
#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:
//! 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<Block>& 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<Block> DecodeFormatMultiblock(const std::string& str, const Configuration::IOBASE_FORMAT base);
private:
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 = "");
};
#endif

View File

@ -1,4 +1,4 @@
#include "ModuleDataFormatter.h"
#include "DataFormatter.h"
#include "Bases.h"
#include <GeneralUtility/BaseConversion.h>
#include <StringTools/StringTools.h>
@ -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<Block>& 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<Block> ModuleDataFormatter::DecodeFormatMultiblock(
std::vector<Block> DataFormatter::DecodeFormatMultiblock(
const std::string& str,
const Configuration::IOBASE_FORMAT base
) {
@ -238,7 +238,7 @@ std::vector<Block> 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<Block> ModuleDataFormatter::DecodeFormatMultiblock(
return blocks;
}
std::string ModuleDataFormatter::Bin2CustomBase(
std::string DataFormatter::Bin2CustomBase(
const std::string& bin,
const std::vector<std::string>& 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<std::string>& 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;

View File

@ -1,4 +1,4 @@
#include "ModulePrepareKey.h"
#include "KeyManager.h"
#include "CommandlineInterface.h"
#include "Configuration.h"
#include <cstring>
@ -12,7 +12,7 @@
#include <unistd.h>
#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;

View File

@ -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;
}

View File

@ -1,4 +1,4 @@
#include <ModuleDataFormatter.h>
#include <DataFormatter.h>
#include <GCrypt/Key.h>
#include <GCrypt/GPrng.h>
#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
);