Added support for a variety of output base formats
This commit is contained in:
parent
de84fba0b6
commit
cdbb52b24c
235
GhettoCryptCLI/BaseConversion.h
Normal file
235
GhettoCryptCLI/BaseConversion.h
Normal file
@ -0,0 +1,235 @@
|
||||
#ifndef GENERALUTILITY_BASECONVERSION_H
|
||||
#define GENERALUTILITY_BASECONVERSION_H
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "ContainerUtility.h"
|
||||
|
||||
namespace Leonetienne {
|
||||
namespace GeneralUtility {
|
||||
|
||||
class BaseConversion {
|
||||
public:
|
||||
//! Will divide a number of arbitrary base in `dividend` by an integer divisor.
|
||||
//! This is a specific helper function for the base conversion functions.
|
||||
//! \tparam T_Container The type of container used for the digitstring
|
||||
//! \param dividend The number to be divided in listlike container-form (such as a string)
|
||||
//! \param divisor The integer divisor
|
||||
//! \param set The set/base of `dividend`
|
||||
//! \return A pair of the result. (result, rest)
|
||||
template<class T_Container>
|
||||
static std::pair<T_Container, int>
|
||||
DigitstringDivision(const T_Container& dividend, const unsigned int divisor, const T_Container& set);
|
||||
|
||||
//! Will convert a number of arbitrary base to base 10
|
||||
//! \tparam T_Container The type of container used for the digitstring
|
||||
//! \param num A listlike container representing the number (such as a string)
|
||||
//! \param set The set/base of the number
|
||||
//! \return A 64-bit integer representing the number
|
||||
template<class T_Container>
|
||||
static std::uint64_t BaseX_2_10(const T_Container& num, const T_Container& set);
|
||||
|
||||
//! Will convert a number to an arbitrary base.
|
||||
//! This just a wrapper for BaseX_2_Y.
|
||||
//! \tparam T_Container The type of container used for the digitstring
|
||||
//! \param num The number to be converted
|
||||
//! \param set The desired set/base for the output to be in. Should be a listlike container (such as a string)
|
||||
//! \param minOutLen The minimum output length. Setting this will result in zero-padded output (Like, 00000001 instead of 1)
|
||||
//! \return `num` in base `set`
|
||||
template<class T_Container>
|
||||
static T_Container
|
||||
Base10_2_X(const std::uint64_t& num, const T_Container& set, const std::uint32_t minOutLen = 1);
|
||||
|
||||
//! Will convert a number from an arbitrary base to another arbitrary base.
|
||||
//! \tparam T_ContainerIn The type of container used for the incoming digitstring
|
||||
//! \tparam T_ContainerOut The type of container used for the outgoing digitstring
|
||||
//! \param num A representation of a number in a listlike container (such as a string)
|
||||
//! \param set_in The set/base of the input
|
||||
//! \param set_out The desired set/base to output
|
||||
//! \param minOutLen The minimum output length. Setting this will result in zero-padded output (Like, 00000001 instead of 1)
|
||||
//! \return `num` in base `set_out`
|
||||
template<class T_ContainerIn, class T_ContainerOut>
|
||||
static T_ContainerOut
|
||||
BaseX_2_Y(const T_ContainerIn& num, const T_ContainerIn& set_in, const T_ContainerOut& set_out,
|
||||
const std::uint32_t minOutLen = 1);
|
||||
|
||||
private:
|
||||
// No instantiation! >:(
|
||||
BaseConversion();
|
||||
};
|
||||
|
||||
namespace {
|
||||
// Fast 64-bit int power function
|
||||
inline std::uint64_t Powuli(const std::uint64_t& b, const std::uint64_t& e) {
|
||||
std::uint64_t buf = 1;
|
||||
|
||||
for (std::uint64_t i = 0; i < e; i++)
|
||||
buf *= b;
|
||||
|
||||
return buf;
|
||||
}
|
||||
}
|
||||
|
||||
template<class T_Container>
|
||||
std::uint64_t BaseConversion::BaseX_2_10(const T_Container& num, const T_Container& set) {
|
||||
// If base is 0, throw logic error
|
||||
if (set.size() == 0)
|
||||
throw std::logic_error("Can't convert from base0! Please supply a nonempty set!");
|
||||
|
||||
unsigned long long int buf = 0;
|
||||
for (std::size_t i = 0; i < num.size(); i++) {
|
||||
for (std::size_t j = 0; j < set.size(); j++) {
|
||||
if (num[i] == set[j]) {
|
||||
buf += Powuli((std::uint64_t)set.size(), (uint64_t)(num.size() - (i + 1))) * j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Based on: https://www.geeksforgeeks.org/divide-large-number-represented-string/
|
||||
template<class T_Container>
|
||||
std::pair<T_Container, int>
|
||||
BaseConversion::DigitstringDivision(const T_Container& dividend, const unsigned int divisor,
|
||||
const T_Container& set) {
|
||||
// Quick rejects:
|
||||
|
||||
// No set? Throw logic error
|
||||
if (set.size() == 0)
|
||||
throw std::logic_error("Can't divide a number of base0! Please supply a nonempty set!");
|
||||
|
||||
// No division by 0
|
||||
if (divisor == 0)
|
||||
throw std::overflow_error("Division by zero!");
|
||||
|
||||
// Dividend size 0? Return 0.
|
||||
if (dividend.size() == 0)
|
||||
return std::make_pair(T_Container({ set[0] }), 0);
|
||||
|
||||
// Verify that all digits are represented in the set/base
|
||||
for (const auto& c : dividend)
|
||||
if (ContainerUtility::Ord(c, set) == -1)
|
||||
throw std::logic_error(
|
||||
"The supplied dividend contains a digit that is not represented in the set/base!");
|
||||
|
||||
|
||||
// Now for the actual algorithm:
|
||||
T_Container result;
|
||||
|
||||
// Find prefix of number that is larger than divisor.
|
||||
int idx = 0;
|
||||
int temp = ContainerUtility::Ord(dividend[idx], set);
|
||||
while (temp < divisor) {
|
||||
idx++;
|
||||
if (idx < dividend.size())
|
||||
temp = temp * set.size() + ContainerUtility::Ord(dividend[idx], set);
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
// Repeatedly divide divisor with temp. After
|
||||
// every division, update temp to include one
|
||||
// more digit.
|
||||
int curRest = temp % divisor;
|
||||
while (dividend.size() > idx) {
|
||||
// Store result in answer i.e. temp / divisor
|
||||
result.insert(result.cend(), set[temp / divisor]);
|
||||
curRest = temp % divisor;
|
||||
|
||||
// Take next digit of number
|
||||
idx++;
|
||||
if (idx < dividend.size())
|
||||
temp = (temp % divisor) * set.size() + ContainerUtility::Ord(dividend[idx], set);
|
||||
}
|
||||
|
||||
// If divisor is greater than number
|
||||
if (result.size() == 0) {
|
||||
// Generate 0-value digitstring
|
||||
result.clear();
|
||||
result.insert(result.cend(), set[0]);
|
||||
return std::make_pair(result, BaseX_2_10(dividend, set));
|
||||
}
|
||||
|
||||
// else return the answer
|
||||
return std::make_pair(result, curRest);
|
||||
}
|
||||
|
||||
template<class T_ContainerIn, class T_ContainerOut>
|
||||
T_ContainerOut
|
||||
BaseConversion::BaseX_2_Y(const T_ContainerIn& num, const T_ContainerIn& set_in, const T_ContainerOut& set_out,
|
||||
const std::uint32_t minOutLen) {
|
||||
if ((set_in.size() == 0) || (set_out.size() == 0))
|
||||
throw std::logic_error("Can't convert from or to base0! Please supply a nonempty set!");
|
||||
|
||||
T_ContainerOut result;
|
||||
|
||||
// Generate a 0-value string for inbase
|
||||
const T_ContainerIn zeroInbase({ set_in[0] });
|
||||
|
||||
if (num != zeroInbase) {
|
||||
// Populate result object
|
||||
{
|
||||
T_ContainerIn buf = num;
|
||||
while (buf != zeroInbase) {
|
||||
const auto divRes = DigitstringDivision(buf, set_out.size(), set_in);
|
||||
const std::uint64_t mod = divRes.second;
|
||||
buf = divRes.first;
|
||||
result.insert(result.cend(), set_out[mod]);
|
||||
}
|
||||
}
|
||||
|
||||
// Reverse result object item order
|
||||
{
|
||||
// Now reverse result
|
||||
T_ContainerOut buf = result;
|
||||
result.clear();
|
||||
for (std::size_t i = 0; i < buf.size(); i++)
|
||||
result.insert(result.cend(), buf[buf.size() - i - 1]);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// If num is 0, just pass a null-value. The algorithm would hang otherwise.
|
||||
result.insert(result.cend(), set_out[0]);
|
||||
}
|
||||
|
||||
|
||||
// Add as much null-values to the left as requested.
|
||||
if (result.size() < minOutLen) {
|
||||
const std::size_t cachedLen = result.size();
|
||||
const T_ContainerOut cachedStr = result;
|
||||
result.clear();
|
||||
for (std::size_t i = 0; i < minOutLen - cachedLen; i++)
|
||||
result.insert(result.cend(), set_out[0]);
|
||||
|
||||
for (const auto& it : cachedStr)
|
||||
result.insert(result.cend(), it);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class T_Container>
|
||||
T_Container
|
||||
BaseConversion::Base10_2_X(const std::uint64_t& num, const T_Container& set, const std::uint32_t minOutLen) {
|
||||
// Convert num to a string
|
||||
std::stringstream ss;
|
||||
ss << num;
|
||||
const std::string numStr = ss.str();
|
||||
|
||||
// Use BaseX_2_Y to convert to outbase
|
||||
const T_Container convertedNum = BaseX_2_Y<std::string, T_Container>(numStr, "0123456789", set, minOutLen);
|
||||
|
||||
// return it
|
||||
return convertedNum;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif //GENERALUTILITY_BASECONVERSION_H
|
1101
GhettoCryptCLI/Bases.h
Normal file
1101
GhettoCryptCLI/Bases.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -63,6 +63,24 @@ void CommandlineInterface::Init(int argc, const char* const* argv)
|
||||
nupp.RegisterConstraint("--progress", ParamConstraint(true, DATA_TYPE::VOID, {}, false, {}));
|
||||
nupp.RegisterAbbreviation("-p", "--progress");
|
||||
|
||||
nupp.RegisterDescription("--iobase-2", "Interpret and format ciphertexts in base2");
|
||||
nupp.RegisterConstraint("--iobase-2", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-8", "--iobase-10", "--iobase-64", "--iobase-uwu", "--iobase-emoji" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-8", "Interpret and format ciphertexts in base8");
|
||||
nupp.RegisterConstraint("--iobase-8", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-10", "--iobase-64", "--iobase-uwu", "--iobase-emoji" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-10", "Interpret and format ciphertexts in base10");
|
||||
nupp.RegisterConstraint("--iobase-10", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-64", "--iobase-uwu", "--iobase-emoji" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-64", "Interpret and format ciphertexts in base64");
|
||||
nupp.RegisterConstraint("--iobase-64", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-uwu", "--iobase-emoji" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-uwu", "Interpret and format ciphertexts in base uwu");
|
||||
nupp.RegisterConstraint("--iobase-uwu", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-64", "--iobase-emoji" }));
|
||||
|
||||
nupp.RegisterDescription("--iobase-emoji", "Interpret and format ciphertexts in base emoji (EXPERIMENTAL)");
|
||||
nupp.RegisterConstraint("--iobase-emoji", ParamConstraint(true, DATA_TYPE::VOID, {}, false, { "--iobase-2", "--iobase-8", "--iobase-10", "--iobase-64", "--iobase-uwu" }));
|
||||
|
||||
nupp.RegisterDescription("--version", "Will supply the version of ghettocrypt used.");
|
||||
nupp.RegisterConstraint("--version", ParamConstraint(true, DATA_TYPE::VOID, {}, false, {}));
|
||||
nupp.RegisterAbbreviation("-v", "--version");
|
||||
|
41
GhettoCryptCLI/ContainerUtility.h
Normal file
41
GhettoCryptCLI/ContainerUtility.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef GENERALUTILITY_CONTAINERUTILITY_H
|
||||
#define GENERALUTILITY_CONTAINERUTILITY_H
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace Leonetienne {
|
||||
namespace GeneralUtility {
|
||||
|
||||
class ContainerUtility {
|
||||
public:
|
||||
//! Will return the index of `item` in `set`.
|
||||
//! \tparam T_Type The type of `item`
|
||||
//! \tparam T_Container The type of container
|
||||
//! \param item The item to find the index for
|
||||
//! \param set The container to be looking in
|
||||
//! \return The index of `item` in `set`. -1 if not found.
|
||||
template<typename T_Type, class T_Container>
|
||||
static int Ord(const T_Type& item, const T_Container& set);
|
||||
|
||||
private:
|
||||
// No instantiation! >:(
|
||||
ContainerUtility();
|
||||
};
|
||||
|
||||
template<typename T_Type, class T_Container>
|
||||
int ContainerUtility::Ord(const T_Type& item, const T_Container& set) {
|
||||
const auto result =
|
||||
std::find_if(set.begin(), set.end(), [item](const T_Type& c) -> bool {
|
||||
return c == item;
|
||||
});
|
||||
|
||||
// No item found
|
||||
if (result == set.end())
|
||||
return -1;
|
||||
else
|
||||
return result - set.begin();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif //GENERALUTILITY_CONTAINERUTILITY_H
|
@ -149,7 +149,9 @@
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="BaseConversion.h" />
|
||||
<ClInclude Include="CommandlineInterface.h" />
|
||||
<ClInclude Include="ContainerUtility.h" />
|
||||
<ClInclude Include="Hazelnupp.h" />
|
||||
<ClInclude Include="Version.h" />
|
||||
</ItemGroup>
|
||||
|
@ -35,5 +35,11 @@
|
||||
<ClInclude Include="Version.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="BaseConversion.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="ContainerUtility.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -1,6 +1,8 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include "CommandlineInterface.h"
|
||||
#include "../GhettoCrypt/Util.h"
|
||||
#include "../GhettoCrypt/Config.h"
|
||||
@ -8,6 +10,10 @@
|
||||
#include "../GhettoCrypt/Flexblock.h"
|
||||
#include "../GhettoCrypt/Block.h"
|
||||
|
||||
#include "Bases.h"
|
||||
#include "BaseConversion.h"
|
||||
|
||||
// Required for hidden password input
|
||||
#if defined _WIN32 || defined _WIN64
|
||||
#include <Windows.h>
|
||||
#elif defined __GNUG__
|
||||
@ -17,6 +23,29 @@
|
||||
|
||||
using namespace GhettoCipher;
|
||||
|
||||
namespace {
|
||||
template <class T_CONTAINER>
|
||||
inline std::string Hex2CustomBase(const std::string& hex, const T_CONTAINER& customSet, const std::string& seperator = "")
|
||||
{
|
||||
std::stringstream ss;
|
||||
|
||||
// Translate to custom set
|
||||
const T_CONTAINER vec_base_custom =
|
||||
Leonetienne::GeneralUtility::BaseConversion::BaseX_2_Y<std::string, T_CONTAINER>(hex, BASE_16, 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();
|
||||
}
|
||||
}
|
||||
|
||||
//! Will prompt a user password from stdin, hiding the input
|
||||
std::string PasswordPrompt()
|
||||
{
|
||||
@ -190,11 +219,47 @@ int main(int argc, char** argv)
|
||||
else
|
||||
{
|
||||
// Output to stdout as a hexstring
|
||||
if (shouldEncrypt)
|
||||
std::cout << BitsToHexstring(output) << std::endl;
|
||||
if (shouldEncrypt) {
|
||||
|
||||
const std::string hexString = BitsToHexstring(output);
|
||||
|
||||
std::string formattedCiphertext;
|
||||
|
||||
// Are we using iobase 2?
|
||||
if (CommandlineInterface::Get().HasParam("--iobase-2"))
|
||||
// Yes: convert hex to base 2
|
||||
formattedCiphertext = Hex2CustomBase<std::string>(hexString, BASE_2);
|
||||
|
||||
// Are we using iobase 10?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-10"))
|
||||
// Yes: convert hex to base 10
|
||||
formattedCiphertext = Hex2CustomBase<std::string>(hexString, BASE_10);
|
||||
|
||||
// Are we using iobase 64?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-64"))
|
||||
// Yes: convert hex to base 64
|
||||
formattedCiphertext = Hex2CustomBase<std::string>(hexString, BASE_64);
|
||||
|
||||
// Are we using iobase uwu?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-uwu"))
|
||||
// Yes: convert hex to base 64
|
||||
formattedCiphertext = Hex2CustomBase<std::vector<std::string>>(hexString, BASE_UWU, " ");
|
||||
|
||||
// Are we using iobase emoji?
|
||||
else if (CommandlineInterface::Get().HasParam("--iobase-emoji"))
|
||||
// Yes: convert hex to base 64
|
||||
formattedCiphertext = Hex2CustomBase<std::vector<std::string>>(hexString, BASE_EMOJI);
|
||||
|
||||
// Else, no special output format specified, use hex
|
||||
else
|
||||
formattedCiphertext = hexString;
|
||||
|
||||
std::cout << formattedCiphertext << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << BitsToString(output) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user