Moved utility functions to cpp file

This commit is contained in:
Leonetienne 2022-05-22 14:13:10 +02:00
parent fedb30bb43
commit b0e0c0cd99
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
2 changed files with 222 additions and 197 deletions

View File

@ -45,231 +45,47 @@ namespace Leonetienne::GCrypt {
} }
//! Will pad a string to a set length with a certain character //! Will pad a string to a set length with a certain character
inline std::string PadStringToLength(const std::string& str, const std::size_t len, const char pad, const bool padLeft = true) { std::string PadStringToLength(const std::string& str, const std::size_t len, const char pad, const bool padLeft = true);
// Fast-reject: Already above padded length
if (str.length() >= len) {
return str;
}
std::stringstream ss;
// Pad left:
if (padLeft) {
for (std::size_t i = 0; i < len - str.size(); i++) {
ss << pad;
}
ss << str;
}
// Pad right:
else {
ss << str;
for (std::size_t i = 0; i < len - str.size(); i++) {
ss << pad;
}
}
return ss.str();
}
//! Will convert a string to a fixed-size data block //! Will convert a string to a fixed-size data block
//! @s: The string to pad //! @s: The string to pad
//! padLeft: should padding be added to the left? If not, to the right. //! padLeft: should padding be added to the left? If not, to the right.
inline Block StringToBitblock(const std::string& s, bool padLeft = true) { Block StringToBitblock(const std::string& s, bool padLeft = true);
std::stringstream ss;
for (std::size_t i = 0; i < s.size(); i++) {
ss << std::bitset<8>(s[i]);
}
// Pad rest with zeores
return Block(PadStringToLength(ss.str(), BLOCK_SIZE, '0', padLeft));
}
//! Will convert a string to a flexible data block //! Will convert a string to a flexible data block
inline Flexblock StringToBits(const std::string& s) { Flexblock StringToBits(const std::string& s);
std::stringstream ss;
for (std::size_t i = 0; i < s.size(); i++) {
ss << std::bitset<8>(s[i]);
}
return Flexblock(ss.str());
}
//! Will convert a fixed-size data block to a bytestring //! Will convert a fixed-size data block to a bytestring
inline std::string BitblockToBytes(const Block& bits) { std::string BitblockToBytes(const Block& bits);
std::stringstream ss;
const std::string bitstring = bits.to_string();
for (std::size_t i = 0; i < BLOCK_SIZE; i += 8) {
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
}
return ss.str();
}
//! Will convert a fixed-size data block to a string //! Will convert a fixed-size data block to a string
//! The difference to BitblockToBytes() is, that it strips excess nullbytes //! The difference to BitblockToBytes() is, that it strips excess nullbytes
inline std::string BitblockToString(const Block& bits) { std::string BitblockToString(const Block& bits);
// Decode to bytes
std::string text = BitblockToBytes(bits);
// Dümp excess nullbytes
text.resize(strlen(text.data()));
return text;
}
//! Will convert a flexible data block to a bytestring //! Will convert a flexible data block to a bytestring
inline std::string BitsToBytes(const Flexblock& bits) { std::string BitsToBytes(const Flexblock& bits);
std::stringstream ss;
const std::string bitstring = bits;
for (std::size_t i = 0; i < bits.size(); i += 8) {
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
}
return ss.str();
}
//! Will convert a flexible data block to a string //! Will convert a flexible data block to a string
//! The difference to BitsToBytes() is, that it strips excess nullbytes //! The difference to BitsToBytes() is, that it strips excess nullbytes
inline std::string BitsToString(const Flexblock& bits) { std::string BitsToString(const Flexblock& bits);
// Decode to bytes
std::string text = BitsToBytes(bits);
// Dümp excess nullbytes
text.resize(strlen(text.data()));
return text;
}
//! Turns a fixed-size data block into a hex-string //! Turns a fixed-size data block into a hex-string
inline std::string BitblockToHexstring(const Block& b) { std::string BitblockToHexstring(const Block& b);
std::stringstream ss;
const std::string charset = "0123456789abcdef";
const std::string bstr = b.to_string();
for (std::size_t i = 0; i < bstr.size(); i += 4) {
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
}
return ss.str();
}
//! Turns a flexible data block into a hex-string //! Turns a flexible data block into a hex-string
inline std::string BitsToHexstring(const Flexblock& b) { std::string BitsToHexstring(const Flexblock& b);
std::stringstream ss;
const std::string charset = "0123456789abcdef";
const std::string bstr = b;
for (std::size_t i = 0; i < bstr.size(); i += 4) {
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
}
return ss.str();
}
//! Turns a hex string into a fixed-size data block //! Turns a hex string into a fixed-size data block
inline Block HexstringToBitblock(const std::string& hexstring) { Block HexstringToBitblock(const std::string& hexstring);
std::stringstream ss;
for (std::size_t i = 0; i < hexstring.size(); i++) {
const char c = hexstring[i];
// Get value
std::size_t value;
if ((c >= '0') && (c <= '9')) {
// Is it a number?
value = ((std::size_t)c - '0') + 0;
}
else if ((c >= 'a') && (c <= 'f')) {
// Else, it is a lowercase letter
value = ((std::size_t)c - 'a') + 10;
}
else {
throw std::logic_error("non-hex string detected in HexstringToBits()");
}
// Append to our bits
ss << std::bitset<4>(value);
}
return Block(ss.str());
}
//! Turns a hex string into a flexible data block //! Turns a hex string into a flexible data block
inline Flexblock HexstringToBits(const std::string& hexstring) { Flexblock HexstringToBits(const std::string& hexstring);
std::stringstream ss;
for (std::size_t i = 0; i < hexstring.size(); i++) {
const char c = hexstring[i];
// Get value
std::size_t value;
if ((c >= '0') && (c <= '9')) {
// Is it a number?
value = ((std::size_t)c - '0') + 0;
}
else if ((c >= 'a') && (c <= 'f')) {
// Else, it is a lowercase letter
value = ((std::size_t)c - 'a') + 10;
}
else {
throw std::logic_error("non-hex string detected in HexstringToBits()");
}
// Append to our bits
ss << std::bitset<4>(value);
}
return ss.str();
}
//! Will read a file into a flexblock //! Will read a file into a flexblock
inline Flexblock ReadFileToBits(const std::string& filepath) { Flexblock ReadFileToBits(const std::string& filepath);
// Read file
std::ifstream ifs(filepath, std::ios::binary);
if (!ifs.good()) {
throw std::runtime_error("Unable to open ifilestream!");
}
std::stringstream ss;
std::copy(
std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(ss)
);
ifs.close();
const std::string bytes = ss.str();
// Convert bytes to bits
return StringToBits(bytes);
}
//! Will save bits to a binary file //! Will save bits to a binary file
inline void WriteBitsToFile(const std::string& filepath, const Flexblock& bits) { void WriteBitsToFile(const std::string& filepath, const Flexblock& bits);
// Convert bits to bytes
const std::string bytes = BitsToBytes(bits);
// Write bits to file
std::ofstream ofs(filepath, std::ios::binary);
if (!ofs.good()) {
throw std::runtime_error("Unable to open ofilestream!");
}
ofs.write(bytes.data(), bytes.length());
ofs.close();
return;
}
} }
#endif #endif

View File

@ -2,5 +2,214 @@
#include "GCrypt/GHash.h" #include "GCrypt/GHash.h"
namespace Leonetienne::GCrypt { namespace Leonetienne::GCrypt {
std::string PadStringToLength(const std::string& str, const std::size_t len, const char pad, const bool padLeft) {
// Fast-reject: Already above padded length
if (str.length() >= len) {
return str;
}
std::stringstream ss;
// Pad left:
if (padLeft) {
for (std::size_t i = 0; i < len - str.size(); i++) {
ss << pad;
}
ss << str;
}
// Pad right:
else {
ss << str;
for (std::size_t i = 0; i < len - str.size(); i++) {
ss << pad;
}
}
return ss.str();
}
Block StringToBitblock(const std::string& s, bool padLeft) {
std::stringstream ss;
for (std::size_t i = 0; i < s.size(); i++) {
ss << std::bitset<8>(s[i]);
}
// Pad rest with zeores
return Block(PadStringToLength(ss.str(), BLOCK_SIZE, '0', padLeft));
}
Flexblock StringToBits(const std::string& s) {
std::stringstream ss;
for (std::size_t i = 0; i < s.size(); i++) {
ss << std::bitset<8>(s[i]);
}
return Flexblock(ss.str());
}
std::string BitblockToBytes(const Block& bits) {
std::stringstream ss;
const std::string bitstring = bits.to_string();
for (std::size_t i = 0; i < BLOCK_SIZE; i += 8) {
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
}
return ss.str();
}
std::string BitblockToString(const Block& bits) {
// Decode to bytes
std::string text = BitblockToBytes(bits);
// Dümp excess nullbytes
text.resize(strlen(text.data()));
return text;
}
std::string BitsToBytes(const Flexblock& bits) {
std::stringstream ss;
const std::string bitstring = bits;
for (std::size_t i = 0; i < bits.size(); i += 8) {
ss << (char)std::bitset<8>(bitstring.substr(i, 8)).to_ulong();
}
return ss.str();
}
std::string BitsToString(const Flexblock& bits) {
// Decode to bytes
std::string text = BitsToBytes(bits);
// Dümp excess nullbytes
text.resize(strlen(text.data()));
return text;
}
std::string BitblockToHexstring(const Block& b) {
std::stringstream ss;
const std::string charset = "0123456789abcdef";
const std::string bstr = b.to_string();
for (std::size_t i = 0; i < bstr.size(); i += 4) {
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
}
return ss.str();
}
std::string BitsToHexstring(const Flexblock& b) {
std::stringstream ss;
const std::string charset = "0123456789abcdef";
const std::string bstr = b;
for (std::size_t i = 0; i < bstr.size(); i += 4) {
ss << charset[std::bitset<4>(bstr.substr(i, 4)).to_ulong()];
}
return ss.str();
}
Block HexstringToBitblock(const std::string& hexstring) {
std::stringstream ss;
for (std::size_t i = 0; i < hexstring.size(); i++) {
const char c = hexstring[i];
// Get value
std::size_t value;
if ((c >= '0') && (c <= '9')) {
// Is it a number?
value = ((std::size_t)c - '0') + 0;
}
else if ((c >= 'a') && (c <= 'f')) {
// Else, it is a lowercase letter
value = ((std::size_t)c - 'a') + 10;
}
else {
throw std::logic_error("non-hex string detected in HexstringToBits()");
}
// Append to our bits
ss << std::bitset<4>(value);
}
return Block(ss.str());
}
Flexblock HexstringToBits(const std::string& hexstring) {
std::stringstream ss;
for (std::size_t i = 0; i < hexstring.size(); i++) {
const char c = hexstring[i];
// Get value
std::size_t value;
if ((c >= '0') && (c <= '9')) {
// Is it a number?
value = ((std::size_t)c - '0') + 0;
}
else if ((c >= 'a') && (c <= 'f')) {
// Else, it is a lowercase letter
value = ((std::size_t)c - 'a') + 10;
}
else {
throw std::logic_error("non-hex string detected in HexstringToBits()");
}
// Append to our bits
ss << std::bitset<4>(value);
}
return ss.str();
}
Flexblock ReadFileToBits(const std::string& filepath) {
// Read file
std::ifstream ifs(filepath, std::ios::binary);
if (!ifs.good()) {
throw std::runtime_error("Unable to open ifilestream!");
}
std::stringstream ss;
std::copy(
std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(ss)
);
ifs.close();
const std::string bytes = ss.str();
// Convert bytes to bits
return StringToBits(bytes);
}
void WriteBitsToFile(const std::string& filepath, const Flexblock& bits) {
// Convert bits to bytes
const std::string bytes = BitsToBytes(bits);
// Write bits to file
std::ofstream ofs(filepath, std::ios::binary);
if (!ofs.good()) {
throw std::runtime_error("Unable to open ofilestream!");
}
ofs.write(bytes.data(), bytes.length());
ofs.close();
return;
}
} }