A bit of cleanup
This commit is contained in:
parent
b0d0f831d9
commit
5365233b43
55
GCryptCLI/include/DataFormatter.h
Normal file
55
GCryptCLI/include/DataFormatter.h
Normal 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
|
||||||
|
|
@ -1,12 +1,12 @@
|
|||||||
#ifndef GCRYPTCLI_MODULE_PREPAREKEY_H
|
#ifndef GCRYPTCLI_KEYMANAGER_H
|
||||||
#define GCRYPTCLI_MODULE_PREPAREKEY_H
|
#define GCRYPTCLI_KEYMANAGER_H
|
||||||
|
|
||||||
#include <GCrypt/Key.h>
|
#include <GCrypt/Key.h>
|
||||||
|
|
||||||
using namespace Leonetienne::GCrypt;
|
using namespace Leonetienne::GCrypt;
|
||||||
|
|
||||||
// This class has the task to supply the encryption key.
|
// This class has the task to prepare and supply the encryption key.
|
||||||
class ModulePrepareKey {
|
class KeyManager {
|
||||||
public:
|
public:
|
||||||
//! Will prepare the key. Be it from cli, a file, or, random, or whatever.
|
//! Will prepare the key. Be it from cli, a file, or, random, or whatever.
|
||||||
static void PrepareKey();
|
static void PrepareKey();
|
@ -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
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
#include "ModuleDataFormatter.h"
|
#include "DataFormatter.h"
|
||||||
#include "Bases.h"
|
#include "Bases.h"
|
||||||
#include <GeneralUtility/BaseConversion.h>
|
#include <GeneralUtility/BaseConversion.h>
|
||||||
#include <StringTools/StringTools.h>
|
#include <StringTools/StringTools.h>
|
||||||
@ -8,7 +8,7 @@ using namespace Leonetienne::GCrypt;
|
|||||||
using namespace Leonetienne::StringTools;
|
using namespace Leonetienne::StringTools;
|
||||||
using namespace Leonetienne::GeneralUtility;
|
using namespace Leonetienne::GeneralUtility;
|
||||||
|
|
||||||
std::string ModuleDataFormatter::FormatBlock(
|
std::string DataFormatter::FormatBlock(
|
||||||
const Block& block,
|
const Block& block,
|
||||||
const Configuration::IOBASE_FORMAT base
|
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 std::vector<Block>& blocks,
|
||||||
const Configuration::IOBASE_FORMAT base
|
const Configuration::IOBASE_FORMAT base
|
||||||
) {
|
) {
|
||||||
@ -89,7 +89,7 @@ std::string ModuleDataFormatter::FormatBlocks(
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
Block ModuleDataFormatter::DecodeFormat(
|
Block DataFormatter::DecodeFormat(
|
||||||
const std::string& str,
|
const std::string& str,
|
||||||
const Configuration::IOBASE_FORMAT base
|
const Configuration::IOBASE_FORMAT base
|
||||||
) {
|
) {
|
||||||
@ -163,7 +163,7 @@ Block ModuleDataFormatter::DecodeFormat(
|
|||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Block> ModuleDataFormatter::DecodeFormatMultiblock(
|
std::vector<Block> DataFormatter::DecodeFormatMultiblock(
|
||||||
const std::string& str,
|
const std::string& str,
|
||||||
const Configuration::IOBASE_FORMAT base
|
const Configuration::IOBASE_FORMAT base
|
||||||
) {
|
) {
|
||||||
@ -238,7 +238,7 @@ std::vector<Block> ModuleDataFormatter::DecodeFormatMultiblock(
|
|||||||
}
|
}
|
||||||
|
|
||||||
default:
|
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;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -246,7 +246,7 @@ std::vector<Block> ModuleDataFormatter::DecodeFormatMultiblock(
|
|||||||
return blocks;
|
return blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ModuleDataFormatter::Bin2CustomBase(
|
std::string DataFormatter::Bin2CustomBase(
|
||||||
const std::string& bin,
|
const std::string& bin,
|
||||||
const std::vector<std::string>& customSet,
|
const std::vector<std::string>& customSet,
|
||||||
const std::size_t minLen,
|
const std::size_t minLen,
|
||||||
@ -275,7 +275,7 @@ std::string ModuleDataFormatter::Bin2CustomBase(
|
|||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ModuleDataFormatter::CustomBase2Bin(
|
std::string DataFormatter::CustomBase2Bin(
|
||||||
const std::string& in,
|
const std::string& in,
|
||||||
const std::vector<std::string>& customSet,
|
const std::vector<std::string>& customSet,
|
||||||
const std::string& seperator
|
const std::string& seperator
|
||||||
@ -293,7 +293,7 @@ std::string ModuleDataFormatter::CustomBase2Bin(
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (binary.length() != Block::BLOCK_SIZE_BITS) {
|
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;
|
return binary;
|
@ -1,4 +1,4 @@
|
|||||||
#include "ModulePrepareKey.h"
|
#include "KeyManager.h"
|
||||||
#include "CommandlineInterface.h"
|
#include "CommandlineInterface.h"
|
||||||
#include "Configuration.h"
|
#include "Configuration.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -12,7 +12,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void ModulePrepareKey::PrepareKey() {
|
void KeyManager::PrepareKey() {
|
||||||
|
|
||||||
// Special-case: We are hashing:
|
// Special-case: We are hashing:
|
||||||
// no key needed.
|
// no key needed.
|
||||||
@ -67,11 +67,11 @@ void ModulePrepareKey::PrepareKey() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const Key& ModulePrepareKey::GetKey() {
|
const Key& KeyManager::GetKey() {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string ModulePrepareKey::PasswordPrompt() {
|
std::string KeyManager::PasswordPrompt() {
|
||||||
// Disable stdin-echo
|
// Disable stdin-echo
|
||||||
#if defined _WIN32 || defined _WIN64
|
#if defined _WIN32 || defined _WIN64
|
||||||
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
|
||||||
@ -103,5 +103,5 @@ std::string ModulePrepareKey::PasswordPrompt() {
|
|||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
Key ModulePrepareKey::key;
|
Key KeyManager::key;
|
||||||
|
|
@ -1,6 +1,6 @@
|
|||||||
#include "CommandlineInterface.h"
|
#include "CommandlineInterface.h"
|
||||||
#include "Configuration.h"
|
#include "Configuration.h"
|
||||||
#include "ModulePrepareKey.h"
|
#include "KeyManager.h"
|
||||||
|
|
||||||
int main(int argc, char* const* argv) {
|
int main(int argc, char* const* argv) {
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ int main(int argc, char* const* argv) {
|
|||||||
Configuration::Parse();
|
Configuration::Parse();
|
||||||
|
|
||||||
// Prepare the key
|
// Prepare the key
|
||||||
ModulePrepareKey::PrepareKey();
|
KeyManager::PrepareKey();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
#include <ModuleDataFormatter.h>
|
#include <DataFormatter.h>
|
||||||
#include <GCrypt/Key.h>
|
#include <GCrypt/Key.h>
|
||||||
#include <GCrypt/GPrng.h>
|
#include <GCrypt/GPrng.h>
|
||||||
#include "Catch2.h"
|
#include "Catch2.h"
|
||||||
@ -8,7 +8,7 @@ using namespace Leonetienne::GCrypt;
|
|||||||
// Tests that recoding iobase formats works for individual blocks, with random data
|
// Tests that recoding iobase formats works for individual blocks, with random data
|
||||||
TEMPLATE_TEST_CASE_SIG(
|
TEMPLATE_TEST_CASE_SIG(
|
||||||
__FILE__"Data formats can be converted, without changing in value, with indivudual blocks, with random data",
|
__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 formatUnderTest), formatUnderTest),
|
||||||
Configuration::IOBASE_FORMAT::BASE_BYTES,
|
Configuration::IOBASE_FORMAT::BASE_BYTES,
|
||||||
Configuration::IOBASE_FORMAT::BASE_2,
|
Configuration::IOBASE_FORMAT::BASE_2,
|
||||||
@ -34,13 +34,13 @@ TEMPLATE_TEST_CASE_SIG(
|
|||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
// Convert to a custom base
|
// Convert to a custom base
|
||||||
const std::string b_format = ModuleDataFormatter::FormatBlock(
|
const std::string b_format = DataFormatter::FormatBlock(
|
||||||
b_initial,
|
b_initial,
|
||||||
formatUnderTest
|
formatUnderTest
|
||||||
);
|
);
|
||||||
|
|
||||||
// Convert back to a block
|
// Convert back to a block
|
||||||
const Block b_retrieved = ModuleDataFormatter::DecodeFormat(
|
const Block b_retrieved = DataFormatter::DecodeFormat(
|
||||||
b_format,
|
b_format,
|
||||||
formatUnderTest
|
formatUnderTest
|
||||||
);
|
);
|
||||||
@ -54,7 +54,7 @@ TEMPLATE_TEST_CASE_SIG(
|
|||||||
// Tests that recoding iobase format works
|
// Tests that recoding iobase format works
|
||||||
TEMPLATE_TEST_CASE_SIG(
|
TEMPLATE_TEST_CASE_SIG(
|
||||||
__FILE__"Data formats can be converted, without changing in value, with indivudual blocks, with very little data (lots of nullbytes)",
|
__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 formatUnderTest), formatUnderTest),
|
||||||
Configuration::IOBASE_FORMAT::BASE_BYTES,
|
Configuration::IOBASE_FORMAT::BASE_BYTES,
|
||||||
Configuration::IOBASE_FORMAT::BASE_2,
|
Configuration::IOBASE_FORMAT::BASE_2,
|
||||||
@ -72,13 +72,13 @@ TEMPLATE_TEST_CASE_SIG(
|
|||||||
|
|
||||||
// Exercise
|
// Exercise
|
||||||
// Convert to a custom base
|
// Convert to a custom base
|
||||||
const std::string b_format = ModuleDataFormatter::FormatBlock(
|
const std::string b_format = DataFormatter::FormatBlock(
|
||||||
b_initial,
|
b_initial,
|
||||||
formatUnderTest
|
formatUnderTest
|
||||||
);
|
);
|
||||||
|
|
||||||
// Convert back to a block
|
// Convert back to a block
|
||||||
const Block b_retrieved = ModuleDataFormatter::DecodeFormat(
|
const Block b_retrieved = DataFormatter::DecodeFormat(
|
||||||
b_format,
|
b_format,
|
||||||
formatUnderTest
|
formatUnderTest
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user