feat(hex): without overloading; fix(sha256): const uint8_t * -> uint8_t *

This commit is contained in:
2024-09-11 16:47:51 +03:00
parent 050efca4cc
commit 17e2e04a3b
5 changed files with 77 additions and 2 deletions

View File

@ -8,6 +8,7 @@ DEBUG ?= false
LIB = base
OBJS =\
hex\
baseN\
hash/sha256\

View File

@ -6,6 +6,6 @@
#define SHA256_DIGEST_LENGTH 32
namespace hash {
void sha256(const uint8_t *data, uint64_t data_size, const uint8_t *hash) noexcept;
void sha256(const uint8_t *data, uint64_t data_size, uint8_t *hash) noexcept;
std::vector<uint8_t> sha256(const std::vector<uint8_t> &data) noexcept;
}

12
include/base/hex.hpp Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <cstdint>
#include <string>
#include <vector>
namespace hex
{
bool isValid(const std::string &str) noexcept;
std::string encode(const std::vector<uint8_t> &data) noexcept;
std::vector<uint8_t> decode(const std::string &str);
}

View File

@ -163,7 +163,7 @@ static void sha256_final(SHA256_CTX *ctx, uint8_t *hash)
namespace hash
{
void sha256(const uint8_t *data, uint64_t data_size, const uint8_t *hash) noexcept
void sha256(const uint8_t *data, uint64_t data_size, uint8_t *hash) noexcept
{
SHA256_CTX ctx;
sha256_init(&ctx);

62
src/hex.cpp Normal file
View File

@ -0,0 +1,62 @@
#include <stdexcept>
#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 btc::data
{
bool isValid(const std::string &str) noexcept
{
for (int64_t i = str.size() - 1; i >= 0; i--)
{
if (hexmap[(int8_t)str[i]] == -1)
{
return false;
}
}
return true;
}
std::string encode(const std::vector<uint8_t> &data) noexcept
{
std::string str;
for (uint64_t i = 0; i < data.size(); i++)
{
str.push_back(hexdigits[data[i] >> 4]);
str.push_back(hexdigits[data[i] & 0x0F]);
}
return str;
}
std::vector<uint8_t> decode(const std::string &str)
{
if (!hex::isValid(str))
{
throw std::logic_error("hex::decode: out of digits map");
}
std::vector<uint8_t> data;
for (uint64_t i = 0; i < str.size() / 2; i++)
{
data.push_back(hexmap[(int8_t)str[i * 2]] << 4 | hexmap[(int8_t)str[i * 2 + 1]]);
}
return data;
}
}