fix(baseN, hex, sha256): added str_size argument

This commit is contained in:
2024-09-16 17:48:20 +03:00
parent 090e765a58
commit 66cd56fe95
6 changed files with 14 additions and 14 deletions

View File

@ -7,7 +7,7 @@
namespace baseN
{
bool isValid(const char *str, const int8_t *map) noexcept;
bool isValid(const char *str, size_t str_size, const int8_t *map) noexcept;
bool isValid(std::string_view str, const int8_t *map) noexcept;
void encode(const uint8_t *data, uint64_t data_size, char *str, uint8_t base, const char *digits, uint64_t enc_size) noexcept;

View File

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

View File

@ -9,8 +9,8 @@ namespace hex
{
bool isValid(const char *str) noexcept;
bool isValid(std::string_view str) noexcept;
void encode(const uint8_t *data, uint64_t data_size, char *str) noexcept;
void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) noexcept;
std::string encode(std::span<const uint8_t> data) noexcept;
void decode(const char *str, uint8_t *data, uint64_t data_size);
void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size);
std::vector<uint8_t> decode(std::string_view str);
}

View File

@ -6,15 +6,15 @@
namespace baseN
{
bool isValid(const char *str, const int8_t *map) noexcept
bool isValid(const char *str, size_t str_size, const int8_t *map) noexcept
{
std::string_view sv(str);
std::string_view sv(str, str_size);
return std::all_of(sv.begin(), sv.end(), [map](char ch)
{ return map[(int8_t)ch] != -1; });
}
bool isValid(std::string_view str, const int8_t *map) noexcept
{
return baseN::isValid(str.data(), map);
return baseN::isValid(str.data(), str.size(), map);
}
void encode(const uint8_t *data, uint64_t data_size, char *str, uint8_t base, const char *digits, uint64_t enc_size) noexcept
{

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, uint8_t *hash) noexcept
void sha256(const uint8_t *data, size_t data_size, uint8_t *hash) noexcept
{
SHA256_CTX ctx;
sha256_init(&ctx);

View File

@ -35,9 +35,9 @@ namespace hex
{
return baseN::isValid(str, hexmap);
}
void encode(const uint8_t *data, uint64_t data_size, char *str) noexcept
void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) noexcept
{
std::string_view sv(str);
std::string_view sv(str, str_size);
std::span<const uint8_t> dv(data, data_size);
for (size_t i = 0; i < dv.size() && i * 2 + 1 < sv.size(); i++)
@ -49,16 +49,16 @@ namespace hex
std::string encode(std::span<const uint8_t> data) noexcept
{
std::string str(data.size() * 2, ' ');
hex::encode(data.data(), data.size(), str.data());
hex::encode(data.data(), data.size(), str.data(), str.size());
return str;
}
void decode(const char *str, uint8_t *data, uint64_t data_size)
void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size)
{
if (!hex::isValid(str))
{
throw std::logic_error("hex::decode: out of digits map");
}
std::string_view sv(str);
std::string_view sv(str, str_size);
std::span<const uint8_t> dv(data, data_size);
if (sv.size() % 2 != 0)
{
@ -73,7 +73,7 @@ namespace hex
std::vector<uint8_t> decode(std::string_view str)
{
std::vector<uint8_t> data(str.size() / 2);
hex::decode(str.data(), data.data(), data.size());
hex::decode(str.data(), str.size(), data.data(), data.size());
return data;
}
}