5 Commits

12 changed files with 373 additions and 254 deletions

View File

@ -11,6 +11,7 @@ LIB = base
OBJS =\
hex\
baseN\
base58\
base64\
hash/sha256\
@ -19,6 +20,7 @@ TOOLS =\
TESTS =\
test-hex\
test-baseN\
test-base58\
test-base64\
hash/test-sha256\

View File

@ -1,4 +1,6 @@
#include <base/hex.hpp>
#include <base/baseN.hpp>
#include <base/base58.hpp>
#include <base/base64.hpp>
#include <base/hash/sha256.hpp>
#include <base/baseN.hpp>
#include <base/hash/sha256.hpp>
#include <base/hex.hpp>

27
include/base/base58.hpp Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#include <cstdint>
#include <span>
#include <string>
#include <vector>
namespace base58
{
extern const char digits[59];
extern const int8_t map[256];
bool isValid(const char *str, uint64_t str_size) noexcept;
bool isValid(std::string_view str) noexcept;
uint64_t sizeEncoded(std::span<const uint8_t> data) noexcept;
uint64_t sizeDecoded(std::string_view str) noexcept;
void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size) noexcept;
std::string encode(std::span<const uint8_t> data) noexcept;
void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size) noexcept;
std::vector<uint8_t> decode(std::string_view str) noexcept;
std::string encodeCheck(std::span<const uint8_t> data) noexcept;
std::vector<uint8_t> decodeCheck(std::string_view str);
}

View File

@ -7,6 +7,9 @@
namespace base64
{
extern const char digits[65];
extern const int8_t map[256];
bool isValid(const char *str, uint64_t str_size) noexcept;
bool isValid(std::string_view str) noexcept;

View File

@ -7,6 +7,9 @@
namespace hex
{
extern const char digits[17];
extern const int8_t map[256];
bool isValid(const char *str, uint64_t str_size) noexcept;
bool isValid(std::string_view str) noexcept;

80
src/base58.cpp Normal file
View File

@ -0,0 +1,80 @@
#include <stdexcept>
#include <base/base58.hpp>
#include <base/baseN.hpp>
#include <base/hash/sha256.hpp>
namespace base58
{
const char digits[] =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const int8_t map[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1,
-1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1,
-1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
//
};
bool isValid(const char *str, uint64_t str_size) noexcept
{
return baseN::isValid(str, str_size, map);
}
bool isValid(std::string_view str) noexcept
{
return baseN::isValid(str, map);
}
uint64_t sizeEncoded(std::span<const uint8_t> data) noexcept
{
return baseN::sizeEncoded(data, 58);
}
uint64_t sizeDecoded(std::string_view str) noexcept
{
return baseN::sizeDecoded(str, 58);
}
void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size) noexcept
{
baseN::encode(data, data_size, str, str_size, 58, digits);
}
std::string encode(std::span<const uint8_t> data) noexcept
{
return baseN::encode(data, 58, digits);
}
void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size) noexcept
{
baseN::decode(str, str_size, data, data_size, 58, digits, map);
}
std::vector<uint8_t> decode(std::string_view str) noexcept
{
return baseN::decode(str, 58, digits, map);
}
std::string encodeCheck(std::span<const uint8_t> data) noexcept
{
std::vector<uint8_t> buff(data.begin(), data.end()), dhash;
dhash = hash::sha256(hash::sha256(data));
buff.insert(buff.end(), &dhash[0], &dhash[4]);
return base58::encode(buff);
}
std::vector<uint8_t> decodeCheck(std::string_view str)
{
std::vector<uint8_t> buff(base58::decode(str));
std::span<uint8_t> data(buff.begin(), buff.end() - 4);
std::span<uint8_t> dhash(buff.end() - 4, buff.end());
if (!std::equal(dhash.begin(), dhash.end(), hash::sha256(hash::sha256(data)).begin()))
{
throw std::logic_error("base58::decodeCheck: checksum incorrect");
}
return std::vector<uint8_t>(data.begin(), data.end());
}
}

View File

@ -5,31 +5,29 @@
#include <base/base64.hpp>
#include <base/baseN.hpp>
static const char b64digits[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const int8_t b64map[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
//
};
namespace base64
{
const char digits[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const int8_t map[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
//
};
bool isValid(const char *str, uint64_t str_size) noexcept
{
return base64::isValid(std::string_view(str, str_size));
@ -43,7 +41,7 @@ namespace base64
{
return false;
}
return baseN::isValid(sv, b64map);
return baseN::isValid(sv, map);
}
uint64_t sizeEncoded(std::span<const uint8_t> data)
{
@ -70,10 +68,10 @@ namespace base64
}
for (uint64_t i = 0; i < data_size / 3; i++)
{
str[i * 4] = b64digits[data[i * 3] >> 2];
str[i * 4 + 1] = b64digits[(data[i * 3] << 4 | data[i * 3 + 1] >> 4) & 0x3F];
str[i * 4 + 2] = b64digits[(data[i * 3 + 1] << 2 | data[i * 3 + 2] >> 6) & 0x3F];
str[i * 4 + 3] = b64digits[data[i * 3 + 2] & 0x3F];
str[i * 4] = digits[data[i * 3] >> 2];
str[i * 4 + 1] = digits[(data[i * 3] << 4 | data[i * 3 + 1] >> 4) & 0x3F];
str[i * 4 + 2] = digits[(data[i * 3 + 1] << 2 | data[i * 3 + 2] >> 6) & 0x3F];
str[i * 4 + 3] = digits[data[i * 3 + 2] & 0x3F];
}
uint64_t last_idx = data_size / 3 * 4;
if (last_idx + 3 < str_size)
@ -81,15 +79,15 @@ namespace base64
switch (data_size % 3)
{
case 1:
str[last_idx] = b64digits[data[data_size - 1] >> 2];
str[last_idx + 1] = b64digits[data[data_size - 1] << 4 & 0x30];
str[last_idx] = digits[data[data_size - 1] >> 2];
str[last_idx + 1] = digits[data[data_size - 1] << 4 & 0x30];
str[last_idx + 2] = '=';
str[last_idx + 3] = '=';
break;
case 2:
str[last_idx] = b64digits[data[data_size - 2] >> 2];
str[last_idx + 1] = b64digits[(data[data_size - 2] << 4 | data[data_size - 1] >> 4) & 0x3F];
str[last_idx + 2] = b64digits[data[data_size - 1] & 0x0F];
str[last_idx] = digits[data[data_size - 2] >> 2];
str[last_idx + 1] = digits[(data[data_size - 2] << 4 | data[data_size - 1] >> 4) & 0x3F];
str[last_idx + 2] = digits[data[data_size - 1] & 0x0F];
str[last_idx + 3] = '=';
break;
default:
@ -123,19 +121,19 @@ namespace base64
}
for (auto i = 0; i < size / 4; i++)
{
data[i * 3] = b64map[(int8_t)str[i * 4]] << 2 | b64map[(int8_t)str[i * 4 + 1]] >> 4;
data[i * 3 + 1] = b64map[(int8_t)str[i * 4 + 1]] << 4 | b64map[(int8_t)str[i * 4 + 2]] >> 2;
data[i * 3 + 2] = b64map[(int8_t)str[i * 4 + 2]] << 6 | b64map[(int8_t)str[i * 4 + 3]];
data[i * 3] = map[(int8_t)str[i * 4]] << 2 | map[(int8_t)str[i * 4 + 1]] >> 4;
data[i * 3 + 1] = map[(int8_t)str[i * 4 + 1]] << 4 | map[(int8_t)str[i * 4 + 2]] >> 2;
data[i * 3 + 2] = map[(int8_t)str[i * 4 + 2]] << 6 | map[(int8_t)str[i * 4 + 3]];
}
uint64_t last_idx = size / 4 * 3;
switch (size % 4)
{
case 2:
data[last_idx] = b64map[(int8_t)str[size - 2]] << 2 | b64map[(int8_t)str[size - 1]] >> 4;
data[last_idx] = map[(int8_t)str[size - 2]] << 2 | map[(int8_t)str[size - 1]] >> 4;
break;
case 3:
data[last_idx] = b64map[(int8_t)str[size - 3]] << 2 | b64map[(int8_t)str[size - 2]] >> 4;
data[last_idx + 1] = b64map[(int8_t)str[size - 2]] << 4 | b64map[(int8_t)str[size - 1]] >> 2;
data[last_idx] = map[(int8_t)str[size - 3]] << 2 | map[(int8_t)str[size - 2]] >> 4;
data[last_idx + 1] = map[(int8_t)str[size - 2]] << 4 | map[(int8_t)str[size - 1]] >> 2;
break;
default:
break;

View File

@ -11,8 +11,7 @@ namespace baseN
{
bool isValid(const char *str, uint64_t str_size, const int8_t *map) noexcept
{
std::string_view sv(str, str_size);
return std::all_of(sv.begin(), sv.end(), [map](char ch)
return std::all_of(str, str + str_size, [map](char ch)
{ return map[(int8_t)ch] != -1; });
}
bool isValid(std::string_view str, const int8_t *map) noexcept
@ -25,129 +24,105 @@ namespace baseN
{
throw std::overflow_error("baseN::sizeEncoded: overflow");
}
return data.size() * log256 / std::log(base) + 1;
return data.size() * log256 / std::log(base) + 1;
}
uint64_t sizeDecoded(std::string_view str, uint8_t base) noexcept
{
return str.size() * std::log(base) / log256 + 1;
}
// void encode(const uint8_t *data, uint64_t data_size, char *str, uint8_t base, const char *digits, uint64_t enc_size) noexcept
// {
// if (data_size == 0)
// {
// return;
// }
// char res_str[enc_size];
// uint8_t div_buf[data_size];
// std::copy(data, data + data_size, div_buf);
// int64_t
// zero_count = 0,
// idx_div = 0,
// idx_quo = 0,
// idx_quo_last = data_size,
// idx_str = enc_size - 1;
// uint16_t div = data[idx_div++];
void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size, uint8_t base, const char *digits)
{
std::vector<uint8_t> dv(std::find_if(data, data + data_size, [](uint8_t item)
{ return item != 0; }),
data + data_size);
if (dv.size() == 0)
{
return;
}
std::span<char> sv(str, str_size);
auto sv_it = sv.rbegin();
auto dv_it = dv.begin();
auto quo_it = dv.begin();
auto quo_it_last = dv.end();
uint16_t div = *dv_it++;
// while (zero_count < (int64_t)data_size && data[zero_count] == 0)
// {
// zero_count++;
// }
// while (idx_quo_last > 1 || div_buf[0] > base)
// {
// if (div < base)
// {
// div <<= 8;
// div += div_buf[idx_div++];
// }
// div_buf[idx_quo++] = div / base;
// div %= base;
// while (idx_div < idx_quo_last)
// {
// div <<= 8;
// div += div_buf[idx_div++];
// div_buf[idx_quo++] = div / base;
// div %= base;
// }
// idx_quo_last = idx_quo;
// idx_quo = 0;
// idx_div = 0;
// res_str[idx_str--] = digits[div];
// div = div_buf[idx_div++];
// }
// res_str[idx_str--] = digits[div];
// while (zero_count > 0 && idx_str >= 0)
// {
// res_str[idx_str--] = digits[0];
// zero_count--;
// }
// while (idx_str >= 0)
// {
// res_str[idx_str--] = ' ';
// }
// std::copy(res_str, res_str + enc_size, str);
// }
// std::string encode(std::span<const uint8_t> data, uint8_t base, const char *digits, uint64_t enc_size) noexcept
// {
// std::string str(enc_size, ' ');
// baseN::encode(data.data(), data.size(), str.data(), base, digits, enc_size);
// str.erase(str.begin(), std::find_if(
// str.begin(), str.end(), [](char ch)
// { return ch != ' '; }));
// return str;
// }
// std::string encode(std::span<const uint8_t> data, uint8_t base, const char *digits) noexcept
// {
// return baseN::encode(data, base, digits, data.size() * std::log(256) / std::log(base) + 1);
// }
// void decode(const char *str, uint8_t *data, uint64_t data_size, uint8_t base, const char *digits, const int8_t *map, uint64_t dec_size)
// {
// if (str[0] == '\0')
// {
// return;
// }
// if (!baseN::isValid(str, map))
// {
// throw std::logic_error("baseN::decode: out of digits map");
// }
// uint8_t res_data[dec_size];
// uint64_t idx_str = 0;
// int64_t
// zero_count = 0,
// idx_quo = dec_size - 1,
// idx_quo_last = dec_size - 2;
// uint16_t div;
while ((dv[0] > base || quo_it_last > dv.begin() + 1) && sv_it < sv.rend() - 1)
{
if (div < base)
{
div <<= 8;
div += *dv_it++;
}
*quo_it++ = div / base;
div %= base;
while (dv_it < quo_it_last)
{
div <<= 8;
div += *dv_it++;
*quo_it++ = div / base;
div %= base;
}
quo_it_last = quo_it;
dv_it = dv.begin();
quo_it = dv.begin();
*sv_it++ = digits[div];
div = *dv_it++;
}
*sv_it++ = digits[div];
for (uint64_t i = 0; i < data_size - dv.size() && sv_it < sv.rend(); i++)
{
*sv_it++ = digits[0];
}
}
std::string encode(std::span<const uint8_t> data, uint8_t base, const char *digits) noexcept
{
std::string str(baseN::sizeEncoded(data, base), ' ');
baseN::encode(data.data(), data.size(), str.data(), str.size(), base, digits);
str.erase(str.begin(), std::find_if(
str.begin(), str.end(), [](char ch)
{ return ch != ' '; }));
return str;
}
void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size, uint8_t base, const char *digits, const int8_t *map)
{
std::string_view sv(std::find_if(str, str + str_size, [digits](char ch)
{ return ch != digits[0]; }),
str_size);
if (sv.size() == 0)
{
return;
}
if (!baseN::isValid(sv, map))
{
throw std::logic_error("baseN::decode: out of digits map");
}
std::span<uint8_t> dv(data, data_size);
auto sv_it = sv.begin();
auto quo_it = dv.rbegin();
auto quo_it_last = dv.rbegin() + 1;
uint16_t div;
// while (str[zero_count] != '\0' && str[zero_count] == digits[0])
// {
// zero_count++;
// }
// res_data[idx_quo] = map[(int8_t)str[idx_str++]];
// while (str[idx_str] != '\0')
// {
// div = map[(int8_t)str[idx_str++]];
// while (idx_quo > idx_quo_last && idx_quo > 0)
// {
// div += res_data[idx_quo] * base;
// res_data[idx_quo--] = div;
// div >>= 8;
// }
// res_data[idx_quo--] = div;
// idx_quo_last = idx_quo;
// idx_quo = dec_size - 1;
// }
// std::copy(res_data, res_data + std::min(dec_size, data_size), data);
// }
// std::vector<uint8_t> decode(std::string_view str, uint8_t base, const char *digits, const int8_t *map, uint64_t dec_size)
// {
// std::vector<uint8_t> data(dec_size);
// baseN::decode(str.data(), data.data(), data.size(), base, digits, map, dec_size);
// data.erase(data.begin(), std::find_if(
// data.begin(), data.end(), [](uint8_t byte)
// { return byte != 0; }));
// return data;
// }
// std::vector<uint8_t> decode(std::string_view str, uint8_t base, const char *digits, const int8_t *map)
// {
// return baseN::decode(str, base, digits, map, str.size() * std::log(base) / std::log(256) + 1);
// }
*quo_it = map[(int8_t)*sv_it++];
while (sv_it < sv.end())
{
div = map[(int8_t)*sv_it++];
while (quo_it < quo_it_last && quo_it < dv.rend() - 1)
{
div += *quo_it * base;
*quo_it++ = div;
div >>= 8;
}
*quo_it++ = div;
quo_it_last = quo_it;
quo_it = dv.rbegin();
}
}
std::vector<uint8_t> decode(std::string_view str, uint8_t base, const char *digits, const int8_t *map) noexcept
{
std::vector<uint8_t> data(baseN::sizeDecoded(str, base));
baseN::decode(str.data(), str.size(), data.data(), data.size(), base, digits, map);
data.erase(data.begin(), std::find_if(data.begin(), data.end(), [](uint8_t item)
{ return item != 0; }));
return data;
}
}

View File

@ -4,37 +4,35 @@
#include <base/baseN.hpp>
#include <base/hex.hpp>
static const char hexdigits[] = "0123456789abcdef";
static const int8_t hexmap[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
//
};
namespace hex
{
const char digits[] = "0123456789abcdef";
const int8_t map[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
//
};
bool isValid(const char *str, uint64_t str_size) noexcept
{
return baseN::isValid(str, str_size, hexmap);
return baseN::isValid(str, str_size, map);
}
bool isValid(std::string_view str) noexcept
{
return baseN::isValid(str, hexmap);
return baseN::isValid(str, map);
}
uint64_t sizeEncoded(std::span<const uint8_t> data)
{
@ -56,8 +54,8 @@ namespace hex
}
for (uint64_t i = 0; i < data_size; i++)
{
str[i * 2] = hexdigits[data[i] >> 4];
str[i * 2 + 1] = hexdigits[data[i] & 0x0F];
str[i * 2] = digits[data[i] >> 4];
str[i * 2 + 1] = digits[data[i] & 0x0F];
}
}
std::string encode(std::span<const uint8_t> data) noexcept
@ -82,7 +80,7 @@ namespace hex
}
for (uint64_t i = 0; i * 2 < str_size; i++)
{
data[i] = hexmap[(int8_t)str[i * 2]] << 4 | hexmap[(int8_t)str[i * 2 + 1]];
data[i] = map[(int8_t)str[i * 2]] << 4 | map[(int8_t)str[i * 2 + 1]];
}
}
std::vector<uint8_t> decode(std::string_view str) noexcept

35
test/test-base58.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <utility>
#include <base/base58.hpp>
#include <base/hex.hpp>
#include <gtest/gtest.h>
using namespace base58;
std::pair<std::string, std::string> test = {
"003812e515df45235c6bcc2233ac9d0c4ebbd781de",
"167VUagc755PbQoB7cCTfTPjbQ5Nk6fEuD",
};
TEST(base58, encodeCheck)
{
EXPECT_EQ(encodeCheck(hex::decode(test.first)), test.second);
}
TEST(base58, decodeCheck)
{
EXPECT_EQ(test.first, hex::encode(decodeCheck(test.second)));
try
{
decodeCheck("incorrect");
}
catch (const std::exception &e)
{
EXPECT_STREQ(e.what(), "base58::decodeCheck: checksum incorrect");
}
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -1,3 +1,5 @@
#include <utility>
#include <base/base64.hpp>
#include <base/hex.hpp>
#include <gtest/gtest.h>
@ -6,19 +8,26 @@ using namespace base64;
TEST(base64, isValid)
{
EXPECT_TRUE(isValid("12=="));
EXPECT_TRUE(isValid("123="));
EXPECT_TRUE(isValid("1234"));
EXPECT_FALSE(isValid("1==="));
EXPECT_FALSE(isValid("?!*"));
std::vector<std::pair<bool, std::string>> tests = {
{true, "12=="},
{true, "123="},
{true, "1234"},
{false, "1==="},
{false, "?!*"},
};
for (auto it : tests)
EXPECT_EQ(it.first, isValid(it.second));
}
std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{"BKUEpQ==", "04a504a5"},
{"BKUEpQA=", "04a504a500"},
{"BKUEpQAA", "04a504a50000"},
};
TEST(base64, encode)
{
EXPECT_EQ(encode(hex::decode("")), "");
EXPECT_EQ(encode(hex::decode("04a504a5")), "BKUEpQ==");
EXPECT_EQ(encode(hex::decode("04a504a500")), "BKUEpQA=");
EXPECT_EQ(encode(hex::decode("04a504a50000")), "BKUEpQAA");
for (auto it : tests)
EXPECT_EQ(it.first, encode(hex::decode(it.second)));
}
TEST(base64, encode_1e7)
{
@ -27,10 +36,8 @@ TEST(base64, encode_1e7)
}
TEST(base64, decode)
{
EXPECT_EQ(hex::encode(decode("")), "");
EXPECT_EQ(hex::encode(decode("BKUEpQ==")), "04a504a5");
EXPECT_EQ(hex::encode(decode("BKUEpQA=")), "04a504a500");
EXPECT_EQ(hex::encode(decode("BKUEpQAA")), "04a504a50000");
for (auto it : tests)
EXPECT_EQ(hex::encode(decode(it.first)), it.second);
}
TEST(base64, decode_1e7)
{

View File

@ -1,63 +1,52 @@
#include <utility>
#include <base/base58.hpp>
#include <base/baseN.hpp>
#include <base/hex.hpp>
#include <gtest/gtest.h>
using namespace baseN;
static const char b58digits[] =
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
static const int8_t b58map[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, -1, -1, -1, -1, -1,
-1, 9, 10, 11, 12, 13, 14, 15, 16, -1, 17, 18, 19, 20, 21, -1,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1,
-1, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, -1, 44, 45, 46,
47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
//
};
TEST(baseN, isValid)
{
EXPECT_EQ(true, isValid("123", b58map));
EXPECT_EQ(false, isValid("@#$", b58map));
std::vector<std::pair<bool, std::string>> tests = {
{true, "123"},
{false, "@#$"},
};
for (auto it : tests)
EXPECT_EQ(it.first, isValid(it.second, base58::map));
}
std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{"Ky", "044c"},
{"KyK", "f94a"},
{"KyKX", "387ae2"},
{"KyKXa", "0ccbd755"},
{"KyKXaa", "02e62ec963"},
{"4uqWDRyJZUpS6KKwLAiitndmv7TPFt2bfxVVfhJhgTn3Rh6aQtGHQY6PhhNDpCwSNU8a",
"057902f9cebebb68879911002aae743280140a78c4a077405b057902f9cebebb68879911002aae743280140a78c4a077405b"},
};
TEST(baseN, encode)
{
for (auto it : tests)
EXPECT_EQ(it.first, encode(hex::decode(it.second), 58, base58::digits));
}
TEST(baseN, encode_1e3)
{
std::vector<uint8_t> data(1e3);
std::fill(data.begin(), data.end(), 1);
encode(data, 58, base58::digits);
}
TEST(baseN, decode)
{
for (auto it : tests)
EXPECT_EQ(hex::encode(decode(it.first, 58, base58::digits, base58::map)), it.second);
}
TEST(baseN, decode_1e3)
{
std::string str(1e3, '2');
decode(str, 58, base58::digits, base58::map);
}
// TEST(baseN, encode)
// {
// EXPECT_EQ("Ky", encode(hex::decode("044c"), 58, b58digits));
// EXPECT_EQ("KyK", encode(hex::decode("f94a"), 58, b58digits));
// EXPECT_EQ("KyKX", encode(hex::decode("387ae2"), 58, b58digits));
// EXPECT_EQ("KyKXa", encode(hex::decode("0ccbd755"), 58, b58digits));
// EXPECT_EQ("KyKXaa", encode(hex::decode("02e62ec963"), 58, b58digits));
// }
// TEST(baseN, encode_1e3)
// {
// std::vector<uint8_t> data(1e3);
// encode(data, 58, b58digits, data.size() * 138 / 100 + 1);
// }
// TEST(baseN, decode)
// {
// EXPECT_EQ(hex::encode(decode("Ky", 58, b58digits, b58map)), "044c");
// EXPECT_EQ(hex::encode(decode("KyK", 58, b58digits, b58map)), "f94a");
// EXPECT_EQ(hex::encode(decode("KyKX", 58, b58digits, b58map)), "387ae2");
// EXPECT_EQ(hex::encode(decode("KyKXa", 58, b58digits, b58map)), "0ccbd755");
// EXPECT_EQ(hex::encode(decode("KyKXaa", 58, b58digits, b58map)), "02e62ec963");
// }
// TEST(baseN, decode_1e4)
// {
// std::string str(1e4, '1');
// decode(str, 58, b58digits, b58map, str.size() * 733 / 1000 + 1);
// }
int main(int argc, char **argv)
{