diff options
author | alexvru <alexvru@ydb.tech> | 2023-08-15 21:09:36 +0300 |
---|---|---|
committer | alexvru <alexvru@ydb.tech> | 2023-08-15 21:42:49 +0300 |
commit | d6f67906ea5b369b47bce8e0a7125d66114fdbde (patch) | |
tree | c9c44a3a1a396a6cab33e1260c67f2e5b8b76ea4 /library/cpp/openssl/big_integer/big_integer.cpp | |
parent | f096c967c8a4b645763f901c889ca0335a0e5412 (diff) | |
download | ydb-d6f67906ea5b369b47bce8e0a7125d66114fdbde.tar.gz |
Support BS autoconfig KIKIMR-19031
Diffstat (limited to 'library/cpp/openssl/big_integer/big_integer.cpp')
-rw-r--r-- | library/cpp/openssl/big_integer/big_integer.cpp | 61 |
1 files changed, 61 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(); +} |