diff options
| author | Shauren <shauren.trinity@gmail.com> | 2022-05-03 17:03:57 +0200 |
|---|---|---|
| committer | Shauren <shauren.trinity@gmail.com> | 2022-06-16 12:37:00 +0200 |
| commit | 77091ed599bf34782e26ede0c6b6cbd44c23c52d (patch) | |
| tree | e2172d0303f543ca9f974c3c746b62d127e7124f /src/common/Cryptography/CryptoHash.h | |
| parent | e23a9943dd24cc8333f121c65f4ece969cc6be3d (diff) | |
Core/Crypto: Switch away from most deprecated openssl functions and removed upper version limit
(cherry picked from commit bc87f7b337154e683369a3790ee8fd1a7d4cba98)
Diffstat (limited to 'src/common/Cryptography/CryptoHash.h')
| -rw-r--r-- | src/common/Cryptography/CryptoHash.h | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/src/common/Cryptography/CryptoHash.h b/src/common/Cryptography/CryptoHash.h index 56af9740c04..38f2047c30d 100644 --- a/src/common/Cryptography/CryptoHash.h +++ b/src/common/Cryptography/CryptoHash.h @@ -35,10 +35,10 @@ namespace Trinity::Impl typedef EVP_MD const* (*HashCreator)(); #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L - static EVP_MD_CTX* MakeCTX() { return EVP_MD_CTX_create(); } + static EVP_MD_CTX* MakeCTX() noexcept { return EVP_MD_CTX_create(); } static void DestroyCTX(EVP_MD_CTX* ctx) { EVP_MD_CTX_destroy(ctx); } #else - static EVP_MD_CTX* MakeCTX() { return EVP_MD_CTX_new(); } + static EVP_MD_CTX* MakeCTX() noexcept { return EVP_MD_CTX_new(); } static void DestroyCTX(EVP_MD_CTX* ctx) { EVP_MD_CTX_free(ctx); } #endif }; @@ -73,6 +73,16 @@ namespace Trinity::Impl ASSERT(result == 1); } + GenericHash(GenericHash const& right) : _ctx(GenericHashImpl::MakeCTX()) + { + *this = right; + } + + GenericHash(GenericHash&& right) noexcept + { + *this = std::move(right); + } + ~GenericHash() { if (!_ctx) @@ -81,6 +91,27 @@ namespace Trinity::Impl _ctx = nullptr; } + GenericHash& operator=(GenericHash const& right) + { + if (this == &right) + return *this; + + int result = EVP_MD_CTX_copy(_ctx, right._ctx); + ASSERT(result == 1); + _digest = right._digest; + return *this; + } + + GenericHash& operator=(GenericHash&& right) noexcept + { + if (this == &right) + return *this; + + _ctx = std::exchange(right._ctx, GenericHashImpl::MakeCTX()); + _digest = std::exchange(right._digest, Digest{}); + return *this; + } + void UpdateData(uint8 const* data, size_t len) { int result = EVP_DigestUpdate(_ctx, data, len); @@ -98,8 +129,6 @@ namespace Trinity::Impl int result = EVP_DigestFinal_ex(_ctx, _digest.data(), &length); ASSERT(result == 1); ASSERT(length == DIGEST_LENGTH); - GenericHashImpl::DestroyCTX(_ctx); - _ctx = nullptr; } Digest const& GetDigest() const { return _digest; } @@ -112,6 +141,7 @@ namespace Trinity::Impl namespace Trinity::Crypto { + using MD5 = Trinity::Impl::GenericHash<EVP_md5, Constants::MD5_DIGEST_LENGTH_BYTES>; using SHA1 = Trinity::Impl::GenericHash<EVP_sha1, Constants::SHA1_DIGEST_LENGTH_BYTES>; using SHA256 = Trinity::Impl::GenericHash<EVP_sha256, Constants::SHA256_DIGEST_LENGTH_BYTES>; } |
