Compare commits
5 Commits
5a42cf1011
...
817f6c4fb1
| Author | SHA1 | Date | |
|---|---|---|---|
| 817f6c4fb1 | |||
| f2119850ee | |||
| 5822443706 | |||
| 961e54c08b | |||
| 61c0ae3a72 |
2
Makefile
2
Makefile
@ -11,6 +11,7 @@ LIB = base
|
|||||||
OBJS =\
|
OBJS =\
|
||||||
hex\
|
hex\
|
||||||
baseN\
|
baseN\
|
||||||
|
base58\
|
||||||
base64\
|
base64\
|
||||||
hash/sha256\
|
hash/sha256\
|
||||||
|
|
||||||
@ -19,6 +20,7 @@ TOOLS =\
|
|||||||
TESTS =\
|
TESTS =\
|
||||||
test-hex\
|
test-hex\
|
||||||
test-baseN\
|
test-baseN\
|
||||||
|
test-base58\
|
||||||
test-base64\
|
test-base64\
|
||||||
hash/test-sha256\
|
hash/test-sha256\
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
#include <base/hex.hpp>
|
#include <base/base58.hpp>
|
||||||
#include <base/baseN.hpp>
|
|
||||||
#include <base/base64.hpp>
|
#include <base/base64.hpp>
|
||||||
|
#include <base/baseN.hpp>
|
||||||
#include <base/hash/sha256.hpp>
|
#include <base/hash/sha256.hpp>
|
||||||
|
#include <base/hex.hpp>
|
||||||
|
|
||||||
|
|||||||
27
include/base/base58.hpp
Normal file
27
include/base/base58.hpp
Normal 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);
|
||||||
|
}
|
||||||
@ -7,6 +7,9 @@
|
|||||||
|
|
||||||
namespace base64
|
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(const char *str, uint64_t str_size) noexcept;
|
||||||
bool isValid(std::string_view str) noexcept;
|
bool isValid(std::string_view str) noexcept;
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,9 @@
|
|||||||
|
|
||||||
namespace hex
|
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(const char *str, uint64_t str_size) noexcept;
|
||||||
bool isValid(std::string_view str) noexcept;
|
bool isValid(std::string_view str) noexcept;
|
||||||
|
|
||||||
|
|||||||
80
src/base58.cpp
Normal file
80
src/base58.cpp
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,10 +5,11 @@
|
|||||||
#include <base/base64.hpp>
|
#include <base/base64.hpp>
|
||||||
#include <base/baseN.hpp>
|
#include <base/baseN.hpp>
|
||||||
|
|
||||||
static const char b64digits[] =
|
namespace base64
|
||||||
|
{
|
||||||
|
const char digits[] =
|
||||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
const int8_t map[] = {
|
||||||
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, -1, -1, -1, -1, -1,
|
-1, -1, -1, -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,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
|
||||||
@ -26,10 +27,7 @@ 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, -1, -1, -1, -1, -1,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
//
|
//
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace base64
|
|
||||||
{
|
|
||||||
bool isValid(const char *str, uint64_t str_size) noexcept
|
bool isValid(const char *str, uint64_t str_size) noexcept
|
||||||
{
|
{
|
||||||
return base64::isValid(std::string_view(str, str_size));
|
return base64::isValid(std::string_view(str, str_size));
|
||||||
@ -43,7 +41,7 @@ namespace base64
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return baseN::isValid(sv, b64map);
|
return baseN::isValid(sv, map);
|
||||||
}
|
}
|
||||||
uint64_t sizeEncoded(std::span<const uint8_t> data)
|
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++)
|
for (uint64_t i = 0; i < data_size / 3; i++)
|
||||||
{
|
{
|
||||||
str[i * 4] = b64digits[data[i * 3] >> 2];
|
str[i * 4] = digits[data[i * 3] >> 2];
|
||||||
str[i * 4 + 1] = b64digits[(data[i * 3] << 4 | data[i * 3 + 1] >> 4) & 0x3F];
|
str[i * 4 + 1] = digits[(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 + 2] = digits[(data[i * 3 + 1] << 2 | data[i * 3 + 2] >> 6) & 0x3F];
|
||||||
str[i * 4 + 3] = b64digits[data[i * 3 + 2] & 0x3F];
|
str[i * 4 + 3] = digits[data[i * 3 + 2] & 0x3F];
|
||||||
}
|
}
|
||||||
uint64_t last_idx = data_size / 3 * 4;
|
uint64_t last_idx = data_size / 3 * 4;
|
||||||
if (last_idx + 3 < str_size)
|
if (last_idx + 3 < str_size)
|
||||||
@ -81,15 +79,15 @@ namespace base64
|
|||||||
switch (data_size % 3)
|
switch (data_size % 3)
|
||||||
{
|
{
|
||||||
case 1:
|
case 1:
|
||||||
str[last_idx] = b64digits[data[data_size - 1] >> 2];
|
str[last_idx] = digits[data[data_size - 1] >> 2];
|
||||||
str[last_idx + 1] = b64digits[data[data_size - 1] << 4 & 0x30];
|
str[last_idx + 1] = digits[data[data_size - 1] << 4 & 0x30];
|
||||||
str[last_idx + 2] = '=';
|
str[last_idx + 2] = '=';
|
||||||
str[last_idx + 3] = '=';
|
str[last_idx + 3] = '=';
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
str[last_idx] = b64digits[data[data_size - 2] >> 2];
|
str[last_idx] = digits[data[data_size - 2] >> 2];
|
||||||
str[last_idx + 1] = b64digits[(data[data_size - 2] << 4 | data[data_size - 1] >> 4) & 0x3F];
|
str[last_idx + 1] = digits[(data[data_size - 2] << 4 | data[data_size - 1] >> 4) & 0x3F];
|
||||||
str[last_idx + 2] = b64digits[data[data_size - 1] & 0x0F];
|
str[last_idx + 2] = digits[data[data_size - 1] & 0x0F];
|
||||||
str[last_idx + 3] = '=';
|
str[last_idx + 3] = '=';
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -123,19 +121,19 @@ namespace base64
|
|||||||
}
|
}
|
||||||
for (auto i = 0; i < size / 4; i++)
|
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] = map[(int8_t)str[i * 4]] << 2 | map[(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 + 1] = map[(int8_t)str[i * 4 + 1]] << 4 | map[(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 + 2] = map[(int8_t)str[i * 4 + 2]] << 6 | map[(int8_t)str[i * 4 + 3]];
|
||||||
}
|
}
|
||||||
uint64_t last_idx = size / 4 * 3;
|
uint64_t last_idx = size / 4 * 3;
|
||||||
switch (size % 4)
|
switch (size % 4)
|
||||||
{
|
{
|
||||||
case 2:
|
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;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
data[last_idx] = b64map[(int8_t)str[size - 3]] << 2 | b64map[(int8_t)str[size - 2]] >> 4;
|
data[last_idx] = map[(int8_t)str[size - 3]] << 2 | map[(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 + 1] = map[(int8_t)str[size - 2]] << 4 | map[(int8_t)str[size - 1]] >> 2;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|||||||
213
src/baseN.cpp
213
src/baseN.cpp
@ -11,8 +11,7 @@ namespace baseN
|
|||||||
{
|
{
|
||||||
bool isValid(const char *str, uint64_t str_size, const int8_t *map) noexcept
|
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(str, str + str_size, [map](char ch)
|
||||||
return std::all_of(sv.begin(), sv.end(), [map](char ch)
|
|
||||||
{ return map[(int8_t)ch] != -1; });
|
{ return map[(int8_t)ch] != -1; });
|
||||||
}
|
}
|
||||||
bool isValid(std::string_view str, const int8_t *map) noexcept
|
bool isValid(std::string_view str, const int8_t *map) noexcept
|
||||||
@ -31,123 +30,99 @@ namespace baseN
|
|||||||
{
|
{
|
||||||
return str.size() * std::log(base) / log256 + 1;
|
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
|
void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size, uint8_t base, const char *digits)
|
||||||
// {
|
{
|
||||||
// if (data_size == 0)
|
std::vector<uint8_t> dv(std::find_if(data, data + data_size, [](uint8_t item)
|
||||||
// {
|
{ return item != 0; }),
|
||||||
// return;
|
data + data_size);
|
||||||
// }
|
if (dv.size() == 0)
|
||||||
// char res_str[enc_size];
|
{
|
||||||
// uint8_t div_buf[data_size];
|
return;
|
||||||
// std::copy(data, data + data_size, div_buf);
|
}
|
||||||
// int64_t
|
std::span<char> sv(str, str_size);
|
||||||
// zero_count = 0,
|
auto sv_it = sv.rbegin();
|
||||||
// idx_div = 0,
|
auto dv_it = dv.begin();
|
||||||
// idx_quo = 0,
|
auto quo_it = dv.begin();
|
||||||
// idx_quo_last = data_size,
|
auto quo_it_last = dv.end();
|
||||||
// idx_str = enc_size - 1;
|
uint16_t div = *dv_it++;
|
||||||
// uint16_t div = data[idx_div++];
|
|
||||||
|
|
||||||
// while (zero_count < (int64_t)data_size && data[zero_count] == 0)
|
while ((dv[0] > base || quo_it_last > dv.begin() + 1) && sv_it < sv.rend() - 1)
|
||||||
// {
|
{
|
||||||
// zero_count++;
|
if (div < base)
|
||||||
// }
|
{
|
||||||
// while (idx_quo_last > 1 || div_buf[0] > base)
|
div <<= 8;
|
||||||
// {
|
div += *dv_it++;
|
||||||
// if (div < base)
|
}
|
||||||
// {
|
*quo_it++ = div / base;
|
||||||
// div <<= 8;
|
div %= base;
|
||||||
// div += div_buf[idx_div++];
|
while (dv_it < quo_it_last)
|
||||||
// }
|
{
|
||||||
// div_buf[idx_quo++] = div / base;
|
div <<= 8;
|
||||||
// div %= base;
|
div += *dv_it++;
|
||||||
// while (idx_div < idx_quo_last)
|
*quo_it++ = div / base;
|
||||||
// {
|
div %= base;
|
||||||
// div <<= 8;
|
}
|
||||||
// div += div_buf[idx_div++];
|
quo_it_last = quo_it;
|
||||||
// div_buf[idx_quo++] = div / base;
|
dv_it = dv.begin();
|
||||||
// div %= base;
|
quo_it = dv.begin();
|
||||||
// }
|
*sv_it++ = digits[div];
|
||||||
// idx_quo_last = idx_quo;
|
div = *dv_it++;
|
||||||
// idx_quo = 0;
|
}
|
||||||
// idx_div = 0;
|
*sv_it++ = digits[div];
|
||||||
// res_str[idx_str--] = digits[div];
|
for (uint64_t i = 0; i < data_size - dv.size() && sv_it < sv.rend(); i++)
|
||||||
// div = div_buf[idx_div++];
|
{
|
||||||
// }
|
*sv_it++ = digits[0];
|
||||||
// res_str[idx_str--] = digits[div];
|
}
|
||||||
// while (zero_count > 0 && idx_str >= 0)
|
}
|
||||||
// {
|
std::string encode(std::span<const uint8_t> data, uint8_t base, const char *digits) noexcept
|
||||||
// res_str[idx_str--] = digits[0];
|
{
|
||||||
// zero_count--;
|
std::string str(baseN::sizeEncoded(data, base), ' ');
|
||||||
// }
|
baseN::encode(data.data(), data.size(), str.data(), str.size(), base, digits);
|
||||||
// while (idx_str >= 0)
|
str.erase(str.begin(), std::find_if(
|
||||||
// {
|
str.begin(), str.end(), [](char ch)
|
||||||
// res_str[idx_str--] = ' ';
|
{ return ch != ' '; }));
|
||||||
// }
|
return str;
|
||||||
// std::copy(res_str, res_str + enc_size, 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 encode(std::span<const uint8_t> data, uint8_t base, const char *digits, uint64_t enc_size) noexcept
|
{
|
||||||
// {
|
std::string_view sv(std::find_if(str, str + str_size, [digits](char ch)
|
||||||
// std::string str(enc_size, ' ');
|
{ return ch != digits[0]; }),
|
||||||
// baseN::encode(data.data(), data.size(), str.data(), base, digits, enc_size);
|
str_size);
|
||||||
// str.erase(str.begin(), std::find_if(
|
if (sv.size() == 0)
|
||||||
// str.begin(), str.end(), [](char ch)
|
{
|
||||||
// { return ch != ' '; }));
|
return;
|
||||||
// return str;
|
}
|
||||||
// }
|
if (!baseN::isValid(sv, map))
|
||||||
// std::string encode(std::span<const uint8_t> data, uint8_t base, const char *digits) noexcept
|
{
|
||||||
// {
|
throw std::logic_error("baseN::decode: out of digits map");
|
||||||
// return baseN::encode(data, base, digits, data.size() * std::log(256) / std::log(base) + 1);
|
}
|
||||||
// }
|
std::span<uint8_t> dv(data, data_size);
|
||||||
// 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)
|
auto sv_it = sv.begin();
|
||||||
// {
|
auto quo_it = dv.rbegin();
|
||||||
// if (str[0] == '\0')
|
auto quo_it_last = dv.rbegin() + 1;
|
||||||
// {
|
uint16_t div;
|
||||||
// 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 (str[zero_count] != '\0' && str[zero_count] == digits[0])
|
*quo_it = map[(int8_t)*sv_it++];
|
||||||
// {
|
while (sv_it < sv.end())
|
||||||
// zero_count++;
|
{
|
||||||
// }
|
div = map[(int8_t)*sv_it++];
|
||||||
// res_data[idx_quo] = map[(int8_t)str[idx_str++]];
|
while (quo_it < quo_it_last && quo_it < dv.rend() - 1)
|
||||||
// while (str[idx_str] != '\0')
|
{
|
||||||
// {
|
div += *quo_it * base;
|
||||||
// div = map[(int8_t)str[idx_str++]];
|
*quo_it++ = div;
|
||||||
// while (idx_quo > idx_quo_last && idx_quo > 0)
|
div >>= 8;
|
||||||
// {
|
}
|
||||||
// div += res_data[idx_quo] * base;
|
*quo_it++ = div;
|
||||||
// res_data[idx_quo--] = div;
|
quo_it_last = quo_it;
|
||||||
// div >>= 8;
|
quo_it = dv.rbegin();
|
||||||
// }
|
}
|
||||||
// res_data[idx_quo--] = div;
|
}
|
||||||
// idx_quo_last = idx_quo;
|
std::vector<uint8_t> decode(std::string_view str, uint8_t base, const char *digits, const int8_t *map) noexcept
|
||||||
// idx_quo = dec_size - 1;
|
{
|
||||||
// }
|
std::vector<uint8_t> data(baseN::sizeDecoded(str, base));
|
||||||
// std::copy(res_data, res_data + std::min(dec_size, data_size), data);
|
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)
|
||||||
// std::vector<uint8_t> decode(std::string_view str, uint8_t base, const char *digits, const int8_t *map, uint64_t dec_size)
|
{ return item != 0; }));
|
||||||
// {
|
return data;
|
||||||
// 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);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
22
src/hex.cpp
22
src/hex.cpp
@ -4,9 +4,10 @@
|
|||||||
#include <base/baseN.hpp>
|
#include <base/baseN.hpp>
|
||||||
#include <base/hex.hpp>
|
#include <base/hex.hpp>
|
||||||
|
|
||||||
static const char hexdigits[] = "0123456789abcdef";
|
namespace hex
|
||||||
|
{
|
||||||
static const int8_t hexmap[] = {
|
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,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
@ -24,17 +25,14 @@ 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,
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||||
//
|
//
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace hex
|
|
||||||
{
|
|
||||||
bool isValid(const char *str, uint64_t str_size) noexcept
|
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
|
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)
|
uint64_t sizeEncoded(std::span<const uint8_t> data)
|
||||||
{
|
{
|
||||||
@ -56,8 +54,8 @@ namespace hex
|
|||||||
}
|
}
|
||||||
for (uint64_t i = 0; i < data_size; i++)
|
for (uint64_t i = 0; i < data_size; i++)
|
||||||
{
|
{
|
||||||
str[i * 2] = hexdigits[data[i] >> 4];
|
str[i * 2] = digits[data[i] >> 4];
|
||||||
str[i * 2 + 1] = hexdigits[data[i] & 0x0F];
|
str[i * 2 + 1] = digits[data[i] & 0x0F];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::string encode(std::span<const uint8_t> data) noexcept
|
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++)
|
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
|
std::vector<uint8_t> decode(std::string_view str) noexcept
|
||||||
|
|||||||
35
test/test-base58.cpp
Normal file
35
test/test-base58.cpp
Normal 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();
|
||||||
|
}
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
#include <utility>
|
||||||
|
|
||||||
#include <base/base64.hpp>
|
#include <base/base64.hpp>
|
||||||
#include <base/hex.hpp>
|
#include <base/hex.hpp>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
@ -6,19 +8,26 @@ using namespace base64;
|
|||||||
|
|
||||||
TEST(base64, isValid)
|
TEST(base64, isValid)
|
||||||
{
|
{
|
||||||
EXPECT_TRUE(isValid("12=="));
|
std::vector<std::pair<bool, std::string>> tests = {
|
||||||
EXPECT_TRUE(isValid("123="));
|
{true, "12=="},
|
||||||
EXPECT_TRUE(isValid("1234"));
|
{true, "123="},
|
||||||
|
{true, "1234"},
|
||||||
EXPECT_FALSE(isValid("1==="));
|
{false, "1==="},
|
||||||
EXPECT_FALSE(isValid("?!*"));
|
{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)
|
TEST(base64, encode)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(encode(hex::decode("")), "");
|
for (auto it : tests)
|
||||||
EXPECT_EQ(encode(hex::decode("04a504a5")), "BKUEpQ==");
|
EXPECT_EQ(it.first, encode(hex::decode(it.second)));
|
||||||
EXPECT_EQ(encode(hex::decode("04a504a500")), "BKUEpQA=");
|
|
||||||
EXPECT_EQ(encode(hex::decode("04a504a50000")), "BKUEpQAA");
|
|
||||||
}
|
}
|
||||||
TEST(base64, encode_1e7)
|
TEST(base64, encode_1e7)
|
||||||
{
|
{
|
||||||
@ -27,10 +36,8 @@ TEST(base64, encode_1e7)
|
|||||||
}
|
}
|
||||||
TEST(base64, decode)
|
TEST(base64, decode)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(hex::encode(decode("")), "");
|
for (auto it : tests)
|
||||||
EXPECT_EQ(hex::encode(decode("BKUEpQ==")), "04a504a5");
|
EXPECT_EQ(hex::encode(decode(it.first)), it.second);
|
||||||
EXPECT_EQ(hex::encode(decode("BKUEpQA=")), "04a504a500");
|
|
||||||
EXPECT_EQ(hex::encode(decode("BKUEpQAA")), "04a504a50000");
|
|
||||||
}
|
}
|
||||||
TEST(base64, decode_1e7)
|
TEST(base64, decode_1e7)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,63 +1,52 @@
|
|||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#include <base/base58.hpp>
|
||||||
#include <base/baseN.hpp>
|
#include <base/baseN.hpp>
|
||||||
#include <base/hex.hpp>
|
#include <base/hex.hpp>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
using namespace baseN;
|
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)
|
TEST(baseN, isValid)
|
||||||
{
|
{
|
||||||
EXPECT_EQ(true, isValid("123", b58map));
|
std::vector<std::pair<bool, std::string>> tests = {
|
||||||
EXPECT_EQ(false, isValid("@#$", b58map));
|
{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)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user