From 17e2e04a3b594fc3efed31307c785bb3559188ab Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Wed, 11 Sep 2024 16:47:51 +0300 Subject: [PATCH] feat(hex): without overloading; fix(sha256): const uint8_t * -> uint8_t * --- Makefile | 1 + include/base/hash/sha256.hpp | 2 +- include/base/hex.hpp | 12 +++++++ src/hash/sha256.cpp | 2 +- src/hex.cpp | 62 ++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 include/base/hex.hpp create mode 100644 src/hex.cpp diff --git a/Makefile b/Makefile index 9d47866..91c9a03 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ DEBUG ?= false LIB = base OBJS =\ + hex\ baseN\ hash/sha256\ diff --git a/include/base/hash/sha256.hpp b/include/base/hash/sha256.hpp index e08be57..620c0af 100644 --- a/include/base/hash/sha256.hpp +++ b/include/base/hash/sha256.hpp @@ -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 sha256(const std::vector &data) noexcept; } \ No newline at end of file diff --git a/include/base/hex.hpp b/include/base/hex.hpp new file mode 100644 index 0000000..495f204 --- /dev/null +++ b/include/base/hex.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include +#include +#include + +namespace hex +{ + bool isValid(const std::string &str) noexcept; + std::string encode(const std::vector &data) noexcept; + std::vector decode(const std::string &str); +} \ No newline at end of file diff --git a/src/hash/sha256.cpp b/src/hash/sha256.cpp index 0703130..cad5e54 100644 --- a/src/hash/sha256.cpp +++ b/src/hash/sha256.cpp @@ -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); diff --git a/src/hex.cpp b/src/hex.cpp new file mode 100644 index 0000000..39dee34 --- /dev/null +++ b/src/hex.cpp @@ -0,0 +1,62 @@ +#include + +#include + +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 &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 decode(const std::string &str) + { + if (!hex::isValid(str)) + { + throw std::logic_error("hex::decode: out of digits map"); + } + std::vector 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; + } +} \ No newline at end of file