diff options
| author | Shauren <shauren.trinity@gmail.com> | 2022-05-03 17:03:57 +0200 |
|---|---|---|
| committer | Shauren <shauren.trinity@gmail.com> | 2022-05-03 17:03:57 +0200 |
| commit | bc87f7b337154e683369a3790ee8fd1a7d4cba98 (patch) | |
| tree | f956c8c358f39fc30b74f790b66fcc1c01581546 /src/common/Cryptography/CryptoHash.h | |
| parent | c68f52568f3dff0f5e7cb572ad91e79b23694645 (diff) | |
Core/Crypto: Switch away from most deprecated openssl functions and removed upper version limit
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 8e6f2b32394..d36d345b88c 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>; } |
