diff options
author | Kargatum <dowlandtop@yandex.com> | 2021-06-25 00:50:18 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-24 19:50:18 +0200 |
commit | 3c24b511f2fb2b23618e9cd0b24d28cb5afa34ad (patch) | |
tree | 78baa1669874acf8b7adf1c1994e383e814a3146 /src/common/Cryptography/CryptoHash.h | |
parent | 33c271cc7c191f72406873d81abd954525d093c0 (diff) |
refactor(Core/Misc): add braces and impove codestyle (#6402)
Diffstat (limited to 'src/common/Cryptography/CryptoHash.h')
-rw-r--r-- | src/common/Cryptography/CryptoHash.h | 120 |
1 files changed, 61 insertions, 59 deletions
diff --git a/src/common/Cryptography/CryptoHash.h b/src/common/Cryptography/CryptoHash.h index 3da687c7a4..eab2abec52 100644 --- a/src/common/Cryptography/CryptoHash.h +++ b/src/common/Cryptography/CryptoHash.h @@ -34,67 +34,69 @@ namespace Acore::Impl template <GenericHashImpl::HashCreator HashCreator, size_t DigestLength> class GenericHash { - public: - static constexpr size_t DIGEST_LENGTH = DigestLength; - using Digest = std::array<uint8, DIGEST_LENGTH>; - - static Digest GetDigestOf(uint8 const* data, size_t len) - { - GenericHash hash; - hash.UpdateData(data, len); - hash.Finalize(); - return hash.GetDigest(); - } - - template <typename... Ts> - static auto GetDigestOf(Ts&&... pack) -> std::enable_if_t<!(std::is_integral_v<std::decay_t<Ts>> || ...), Digest> - { - GenericHash hash; - (hash.UpdateData(std::forward<Ts>(pack)), ...); - hash.Finalize(); - return hash.GetDigest(); - } - - GenericHash() : _ctx(GenericHashImpl::MakeCTX()) - { - int result = EVP_DigestInit_ex(_ctx, HashCreator(), nullptr); - ASSERT(result == 1); - } - - ~GenericHash() + public: + static constexpr size_t DIGEST_LENGTH = DigestLength; + using Digest = std::array<uint8, DIGEST_LENGTH>; + + static Digest GetDigestOf(uint8 const* data, size_t len) + { + GenericHash hash; + hash.UpdateData(data, len); + hash.Finalize(); + return hash.GetDigest(); + } + + template <typename... Ts> + static auto GetDigestOf(Ts&& ... pack) -> std::enable_if_t < !(std::is_integral_v<std::decay_t<Ts>> || ...), Digest > + { + GenericHash hash; + (hash.UpdateData(std::forward<Ts>(pack)), ...); + hash.Finalize(); + return hash.GetDigest(); + } + + GenericHash() : _ctx(GenericHashImpl::MakeCTX()) + { + int result = EVP_DigestInit_ex(_ctx, HashCreator(), nullptr); + ASSERT(result == 1); + } + + ~GenericHash() + { + if (!_ctx) { - if (!_ctx) - return; - GenericHashImpl::DestroyCTX(_ctx); - _ctx = nullptr; + return; } - - void UpdateData(uint8 const* data, size_t len) - { - int result = EVP_DigestUpdate(_ctx, data, len); - ASSERT(result == 1); - } - void UpdateData(std::string_view str) { UpdateData(reinterpret_cast<uint8 const*>(str.data()), str.size()); } - void UpdateData(std::string const& str) { UpdateData(std::string_view(str)); } /* explicit overload to avoid using the container template */ - void UpdateData(char const* str) { UpdateData(std::string_view(str)); } /* explicit overload to avoid using the container template */ - template <typename Container> - void UpdateData(Container const& c) { UpdateData(std::data(c), std::size(c)); } - - void Finalize() - { - uint32 length; - 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; } - - private: - EVP_MD_CTX* _ctx; - Digest _digest = { }; + GenericHashImpl::DestroyCTX(_ctx); + _ctx = nullptr; + } + + void UpdateData(uint8 const* data, size_t len) + { + int result = EVP_DigestUpdate(_ctx, data, len); + ASSERT(result == 1); + } + void UpdateData(std::string_view str) { UpdateData(reinterpret_cast<uint8 const*>(str.data()), str.size()); } + void UpdateData(std::string const& str) { UpdateData(std::string_view(str)); } /* explicit overload to avoid using the container template */ + void UpdateData(char const* str) { UpdateData(std::string_view(str)); } /* explicit overload to avoid using the container template */ + template <typename Container> + void UpdateData(Container const& c) { UpdateData(std::data(c), std::size(c)); } + + void Finalize() + { + uint32 length; + 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; } + + private: + EVP_MD_CTX* _ctx; + Digest _digest = { }; }; } |