fix(baseN, hex, sha256): added str_size argument
This commit is contained in:
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
namespace baseN
|
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;
|
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;
|
void encode(const uint8_t *data, uint64_t data_size, char *str, uint8_t base, const char *digits, uint64_t enc_size) noexcept;
|
||||||
|
|||||||
@ -7,6 +7,6 @@
|
|||||||
#define SHA256_DIGEST_LENGTH 32
|
#define SHA256_DIGEST_LENGTH 32
|
||||||
|
|
||||||
namespace 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;
|
||||||
std::vector<uint8_t> sha256(std::span<const uint8_t> data) noexcept;
|
std::vector<uint8_t> sha256(std::span<const uint8_t> data) noexcept;
|
||||||
}
|
}
|
||||||
@ -9,8 +9,8 @@ namespace hex
|
|||||||
{
|
{
|
||||||
bool isValid(const char *str) noexcept;
|
bool isValid(const char *str) noexcept;
|
||||||
bool isValid(std::string_view 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;
|
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);
|
std::vector<uint8_t> decode(std::string_view str);
|
||||||
}
|
}
|
||||||
@ -6,15 +6,15 @@
|
|||||||
|
|
||||||
namespace baseN
|
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 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
|
||||||
{
|
{
|
||||||
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
|
void encode(const uint8_t *data, uint64_t data_size, char *str, uint8_t base, const char *digits, uint64_t enc_size) noexcept
|
||||||
{
|
{
|
||||||
|
|||||||
@ -163,7 +163,7 @@ static void sha256_final(SHA256_CTX *ctx, uint8_t *hash)
|
|||||||
|
|
||||||
namespace 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_CTX ctx;
|
||||||
sha256_init(&ctx);
|
sha256_init(&ctx);
|
||||||
|
|||||||
12
src/hex.cpp
12
src/hex.cpp
@ -35,9 +35,9 @@ namespace hex
|
|||||||
{
|
{
|
||||||
return baseN::isValid(str, hexmap);
|
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);
|
std::span<const uint8_t> dv(data, data_size);
|
||||||
|
|
||||||
for (size_t i = 0; i < dv.size() && i * 2 + 1 < sv.size(); i++)
|
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 encode(std::span<const uint8_t> data) noexcept
|
||||||
{
|
{
|
||||||
std::string str(data.size() * 2, ' ');
|
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;
|
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))
|
if (!hex::isValid(str))
|
||||||
{
|
{
|
||||||
throw std::logic_error("hex::decode: out of digits map");
|
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);
|
std::span<const uint8_t> dv(data, data_size);
|
||||||
if (sv.size() % 2 != 0)
|
if (sv.size() % 2 != 0)
|
||||||
{
|
{
|
||||||
@ -73,7 +73,7 @@ namespace hex
|
|||||||
std::vector<uint8_t> decode(std::string_view str)
|
std::vector<uint8_t> decode(std::string_view str)
|
||||||
{
|
{
|
||||||
std::vector<uint8_t> data(str.size() / 2);
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user