diff --git a/include/basen/base58.hpp b/include/basen/base58.hpp index 1fe6600..8d8fa06 100644 --- a/include/basen/base58.hpp +++ b/include/basen/base58.hpp @@ -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 data) noexcept; + size_t sizeEncoded(std::span 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 data) noexcept; + size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size); + std::string encode(std::span 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 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 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 data) noexcept; + std::string encodeCheck(std::span 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 decodeCheck(std::string_view str); } \ No newline at end of file diff --git a/include/basen/base64.hpp b/include/basen/base64.hpp index 8c953d4..ac57815 100644 --- a/include/basen/base64.hpp +++ b/include/basen/base64.hpp @@ -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 data); size_t sizeDecoded(std::string_view str) noexcept; diff --git a/include/basen/hex.hpp b/include/basen/hex.hpp index 3dc2412..e3b231e 100644 --- a/include/basen/hex.hpp +++ b/include/basen/hex.hpp @@ -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 data); size_t sizeDecoded(std::string_view str) noexcept; diff --git a/src/base58.cpp b/src/base58.cpp index edcf0b8..b94f38c 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace base58 @@ -35,7 +36,7 @@ namespace base58 { return baseN::isValid(str, map); } - size_t sizeEncoded(std::span data) noexcept + size_t sizeEncoded(std::span 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 data) noexcept + std::string encode(std::span 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 decode(std::string_view str) noexcept + std::vector decode(std::string_view str) { return baseN::decode(str, 58, digits, map); } - std::string encodeCheck(std::span data) noexcept + std::string encodeCheck(std::span data) { std::vector buff(data.begin(), data.end()), dhash; dhash = hash::sha256(hash::sha256(data)); @@ -71,13 +72,13 @@ namespace base58 std::vector 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 data(buff.begin(), buff.end() - 4); std::span 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(data.begin(), data.end()); } diff --git a/test/test-base58.cpp b/test/test-base58.cpp index 3b2785f..ad7202b 100644 --- a/test/test-base58.cpp +++ b/test/test-base58.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -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)