fix(base58): basen::Exception
This commit is contained in:
@ -17,34 +17,35 @@ namespace base58
|
||||
bool isValid(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @throw std::overflow_error if if there is an overflow
|
||||
* @throw basen::Exception(OVERFLOW) if there is an overflow
|
||||
*/
|
||||
size_t sizeEncoded(std::span<const uint8_t> data) noexcept;
|
||||
size_t sizeEncoded(std::span<const uint8_t> data);
|
||||
size_t sizeDecoded(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @return number of leading chars, which should be trimmed
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
size_t 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;
|
||||
size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size);
|
||||
std::string encode(std::span<const uint8_t> data);
|
||||
|
||||
/**
|
||||
* @return number of leading chars, which should be trimmed
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size) noexcept;
|
||||
std::vector<uint8_t> decode(std::string_view str) noexcept;
|
||||
size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size);
|
||||
std::vector<uint8_t> decode(std::string_view str);
|
||||
|
||||
/**
|
||||
* @param data vector or span of data which you want to encode
|
||||
* @return encoded string + 4 first bytes of double sha256
|
||||
*/
|
||||
std::string encodeCheck(std::span<const uint8_t> data) noexcept;
|
||||
std::string encodeCheck(std::span<const uint8_t> data);
|
||||
/**
|
||||
* @param str string or string_view which you want to decode
|
||||
* @return decoded data without 4 first bytes of double sha256
|
||||
* @throw std::logic_error checksum incorrect
|
||||
* @throw basen::Exception(PADDING) if str size < 4
|
||||
* @throw basen::Exception(CHECKSUM) checksum incorrect
|
||||
*/
|
||||
std::vector<uint8_t> decodeCheck(std::string_view str);
|
||||
}
|
||||
@ -14,7 +14,7 @@ namespace base64
|
||||
bool isValid(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @throw basen::Exception(OVERFLOW) if if there is an overflow
|
||||
* @throw basen::Exception(OVERFLOW) if there is an overflow
|
||||
*/
|
||||
size_t sizeEncoded(std::span<const uint8_t> data);
|
||||
size_t sizeDecoded(std::string_view str) noexcept;
|
||||
|
||||
@ -14,7 +14,7 @@ namespace hex
|
||||
bool isValid(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @throw basen::Exception(OVERFLOW) if if there is an overflow
|
||||
* @throw basen::Exception(OVERFLOW) if there is an overflow
|
||||
*/
|
||||
size_t sizeEncoded(std::span<const uint8_t> data);
|
||||
size_t sizeDecoded(std::string_view str) noexcept;
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
#include <basen/base58.hpp>
|
||||
#include <basen/baseN.hpp>
|
||||
#include <basen/Exception.hpp>
|
||||
#include <basen/hash/sha256.hpp>
|
||||
|
||||
namespace base58
|
||||
@ -35,7 +36,7 @@ namespace base58
|
||||
{
|
||||
return baseN::isValid(str, map);
|
||||
}
|
||||
size_t sizeEncoded(std::span<const uint8_t> data) noexcept
|
||||
size_t sizeEncoded(std::span<const uint8_t> data)
|
||||
{
|
||||
return baseN::sizeEncoded(data, 58);
|
||||
}
|
||||
@ -43,23 +44,23 @@ namespace base58
|
||||
{
|
||||
return baseN::sizeDecoded(str, 58, digits);
|
||||
}
|
||||
size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size) noexcept
|
||||
size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size)
|
||||
{
|
||||
return baseN::encode(data, data_size, str, str_size, 58, digits);
|
||||
}
|
||||
std::string encode(std::span<const uint8_t> data) noexcept
|
||||
std::string encode(std::span<const uint8_t> data)
|
||||
{
|
||||
return baseN::encode(data, 58, digits);
|
||||
}
|
||||
size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size) noexcept
|
||||
size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size)
|
||||
{
|
||||
return baseN::decode(str, str_size, data, data_size, 58, digits, map);
|
||||
}
|
||||
std::vector<uint8_t> decode(std::string_view str) noexcept
|
||||
std::vector<uint8_t> decode(std::string_view str)
|
||||
{
|
||||
return baseN::decode(str, 58, digits, map);
|
||||
}
|
||||
std::string encodeCheck(std::span<const uint8_t> data) noexcept
|
||||
std::string encodeCheck(std::span<const uint8_t> data)
|
||||
{
|
||||
std::vector<uint8_t> buff(data.begin(), data.end()), dhash;
|
||||
dhash = hash::sha256(hash::sha256(data));
|
||||
@ -71,13 +72,13 @@ namespace base58
|
||||
std::vector<uint8_t> buff(base58::decode(str));
|
||||
if (buff.size() < 4)
|
||||
{
|
||||
throw std::logic_error("base58::decodeCheck: incorrect padding");
|
||||
throw basen::Exception(basen::Exception::Code::PADDING);
|
||||
}
|
||||
std::span<uint8_t> data(buff.begin(), buff.end() - 4);
|
||||
std::span<uint8_t> dhash(buff.end() - 4, buff.end());
|
||||
if (!std::equal(dhash.begin(), dhash.end(), hash::sha256(hash::sha256(data)).begin()))
|
||||
{
|
||||
throw std::logic_error("base58::decodeCheck: checksum incorrect");
|
||||
throw basen::Exception(basen::Exception::Code::CHECKSUM);
|
||||
}
|
||||
return std::vector<uint8_t>(data.begin(), data.end());
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#include <utility>
|
||||
|
||||
#include <basen/base58.hpp>
|
||||
#include <basen/Exception.hpp>
|
||||
#include <basen/hex.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
@ -18,14 +19,7 @@ TEST(base58, encodeCheck)
|
||||
TEST(base58, decodeCheck)
|
||||
{
|
||||
EXPECT_EQ(test.first, hex::encode(decodeCheck(test.second)));
|
||||
try
|
||||
{
|
||||
decodeCheck("incorrect");
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
EXPECT_STREQ(e.what(), "base58::decodeCheck: checksum incorrect");
|
||||
}
|
||||
EXPECT_THROW(decodeCheck("incorrect"), basen::Exception);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
|
||||
Reference in New Issue
Block a user