diff options
author | qrort <qrort@yandex-team.com> | 2022-11-30 23:47:12 +0300 |
---|---|---|
committer | qrort <qrort@yandex-team.com> | 2022-11-30 23:47:12 +0300 |
commit | 22f8ae0e3f5d68b92aecccdf96c1d841a0334311 (patch) | |
tree | bffa27765faf54126ad44bcafa89fadecb7a73d7 /library/cpp/openssl/big_integer | |
parent | 332b99e2173f0425444abb759eebcb2fafaa9209 (diff) | |
download | ydb-22f8ae0e3f5d68b92aecccdf96c1d841a0334311.tar.gz |
validate canons without yatest_common
Diffstat (limited to 'library/cpp/openssl/big_integer')
-rw-r--r-- | library/cpp/openssl/big_integer/big_integer.cpp | 61 | ||||
-rw-r--r-- | library/cpp/openssl/big_integer/big_integer.h | 57 |
2 files changed, 118 insertions, 0 deletions
diff --git a/library/cpp/openssl/big_integer/big_integer.cpp b/library/cpp/openssl/big_integer/big_integer.cpp new file mode 100644 index 0000000000..9b6802369a --- /dev/null +++ b/library/cpp/openssl/big_integer/big_integer.cpp @@ -0,0 +1,61 @@ +#include "big_integer.h" + +#include <util/generic/yexception.h> +#include <util/generic/scope.h> +#include <util/stream/output.h> + +#include <openssl/bn.h> + +using namespace NOpenSsl; + +TBigInteger::~TBigInteger() noexcept { + BN_free(Impl_); +} + +TBigInteger TBigInteger::FromULong(ui64 value) { + TBigInteger result(BN_new()); + + Y_ENSURE(result.Impl(), "BN_new() failed"); + Y_ENSURE(BN_set_word(result.Impl(), value) == 1, "BN_set_word() failed"); + + return result; +} + +TBigInteger TBigInteger::FromRegion(const void* ptr, size_t len) { + auto result = BN_bin2bn((ui8*)(ptr), len, nullptr); + + Y_ENSURE(result, "BN_bin2bn() failed"); + + return result; +} + +int TBigInteger::Compare(const TBigInteger& a, const TBigInteger& b) noexcept { + return BN_cmp(a.Impl(), b.Impl()); +} + +size_t TBigInteger::NumBytes() const noexcept { + return BN_num_bytes(Impl_); +} + +size_t TBigInteger::ToRegion(void* to) const noexcept { + const auto ret = BN_bn2bin(Impl_, (unsigned char*)to); + + Y_VERIFY(ret >= 0, "it happens"); + + return ret; +} + +TString TBigInteger::ToDecimalString() const { + auto res = BN_bn2dec(Impl_); + + Y_DEFER { + OPENSSL_free(res); + }; + + return res; +} + +template <> +void Out<TBigInteger>(IOutputStream& out, const TBigInteger& bi) { + out << bi.ToDecimalString(); +} diff --git a/library/cpp/openssl/big_integer/big_integer.h b/library/cpp/openssl/big_integer/big_integer.h new file mode 100644 index 0000000000..07763c5e13 --- /dev/null +++ b/library/cpp/openssl/big_integer/big_integer.h @@ -0,0 +1,57 @@ +#pragma once + +#include <util/generic/ptr.h> +#include <util/generic/strbuf.h> +#include <util/generic/utility.h> +#include <util/generic/string.h> + +struct bignum_st; + +namespace NOpenSsl { + class TBigInteger { + inline TBigInteger(bignum_st* impl) noexcept + : Impl_(impl) + { + } + + static int Compare(const TBigInteger& a, const TBigInteger& b) noexcept; + + public: + inline TBigInteger(TBigInteger&& other) noexcept { + Swap(other); + } + + ~TBigInteger() noexcept; + + static TBigInteger FromULong(ui64 value); + static TBigInteger FromRegion(const void* ptr, size_t len); + + inline const bignum_st* Impl() const noexcept { + return Impl_; + } + + inline bignum_st* Impl() noexcept { + return Impl_; + } + + inline void Swap(TBigInteger& other) noexcept { + DoSwap(Impl_, other.Impl_); + } + + inline friend bool operator==(const TBigInteger& a, const TBigInteger& b) noexcept { + return Compare(a, b) == 0; + } + + inline friend bool operator!=(const TBigInteger& a, const TBigInteger& b) noexcept { + return !(a == b); + } + + size_t NumBytes() const noexcept; + size_t ToRegion(void* to) const noexcept; + + TString ToDecimalString() const; + + private: + bignum_st* Impl_ = nullptr; + }; +} |