feat(hex): without overloading; fix(sha256): const uint8_t * -> uint8_t *
This commit is contained in:
@ -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
12
include/base/hex.hpp
Normal 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);
|
||||
}
|
||||
@ -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
62
src/hex.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user