From 2c78f4dd1f52200e7061b809bb472dbcd499962e Mon Sep 17 00:00:00 2001 From: Shauren Date: Tue, 21 Dec 2021 18:29:52 +0100 Subject: Core/Misc: Resolve c++17 TODOs left in code as comments --- src/common/Cryptography/CryptoHash.h | 41 ++++++++-------------------------- src/common/Cryptography/HMAC.h | 43 +++++++++--------------------------- src/common/Utilities/advstd.h | 29 ------------------------ 3 files changed, 19 insertions(+), 94 deletions(-) (limited to 'src/common') diff --git a/src/common/Cryptography/CryptoHash.h b/src/common/Cryptography/CryptoHash.h index 991bf15944e..8e6f2b32394 100644 --- a/src/common/Cryptography/CryptoHash.h +++ b/src/common/Cryptography/CryptoHash.h @@ -23,16 +23,12 @@ #include "Errors.h" #include #include -//#include +#include #include -#include -#include "advstd.h" // data/size class BigNumber; -namespace Trinity -{ -namespace Impl +namespace Trinity::Impl { struct GenericHashImpl { @@ -62,26 +58,11 @@ namespace Impl return hash.GetDigest(); } - private: // c++17 - template - static void UpdateData_OLDCPP(GenericHash& hash, T const& data) - { - hash.UpdateData(data); - } - - template - static void UpdateData_OLDCPP(GenericHash& hash, T const& data, TRest&&... rest) - { - hash.UpdateData(data); - UpdateData_OLDCPP(hash, std::forward(rest)...); - } - - public: template - static auto GetDigestOf(Ts&&... pack) -> std::enable_if_t>...>::value, Digest> + static auto GetDigestOf(Ts&&... pack) -> std::enable_if_t>...>, Digest> { GenericHash hash; - UpdateData_OLDCPP(hash, std::forward(pack)...); + (hash.UpdateData(std::forward(pack)), ...); hash.Finalize(); return hash.GetDigest(); } @@ -105,11 +86,11 @@ namespace Impl int result = EVP_DigestUpdate(_ctx, data, len); ASSERT(result == 1); } - // c++17 void UpdateData(std::string_view str) { UpdateData(reinterpret_cast(str.data()), str.size()); } - void UpdateData(std::string const& str) { UpdateData(str.c_str()); } /* explicit overload to avoid using the container template */ - void UpdateData(char const* str) { UpdateData(reinterpret_cast(str), strlen(str)); } /* explicit overload to avoid using the container template */ + void UpdateData(std::string_view str) { UpdateData(reinterpret_cast(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 - void UpdateData(Container const& c) { UpdateData(advstd::data(c), advstd::size(c)); } + void UpdateData(Container const& c) { UpdateData(std::data(c), std::size(c)); } void Finalize() { @@ -128,15 +109,11 @@ namespace Impl Digest _digest = { }; }; } -} -namespace Trinity -{ -namespace Crypto +namespace Trinity::Crypto { using SHA1 = Trinity::Impl::GenericHash; using SHA256 = Trinity::Impl::GenericHash; } -} #endif diff --git a/src/common/Cryptography/HMAC.h b/src/common/Cryptography/HMAC.h index 6b9d2a081bd..7925fb23a61 100644 --- a/src/common/Cryptography/HMAC.h +++ b/src/common/Cryptography/HMAC.h @@ -23,15 +23,12 @@ #include "Errors.h" #include #include -//#include +#include #include -#include "advstd.h" class BigNumber; -namespace Trinity -{ -namespace Impl +namespace Trinity::Impl { struct HMACImpl { @@ -72,26 +69,11 @@ namespace Impl return hash.GetDigest(); } - private: // c++17 - template - static void UpdateData_OLDCPP(GenericHMAC& hash, T const& data) - { - hash.UpdateData(data); - } - - template - static void UpdateData_OLDCPP(GenericHMAC& hash, T const& data, TRest&&... rest) - { - hash.UpdateData(data); - UpdateData_OLDCPP(hash, std::forward(rest)...); - } - - public: template - static auto GetDigestOf(Container const& seed, Ts&&... pack) -> std::enable_if_t>...>::value, Digest> + static auto GetDigestOf(Container const& seed, Ts&&... pack) -> std::enable_if_t>...>, Digest> { GenericHMAC hash(seed); - UpdateData_OLDCPP(hash, std::forward(pack)...); + (hash.UpdateData(std::forward(pack)), ...); hash.Finalize(); return hash.GetDigest(); } @@ -102,7 +84,7 @@ namespace Impl ASSERT(result == 1); } template - GenericHMAC(Container const& container) : GenericHMAC(container.data(), container.size()) {} + GenericHMAC(Container const& container) : GenericHMAC(std::data(container), std::size(container)) {} ~GenericHMAC() { @@ -117,11 +99,11 @@ namespace Impl int result = HMAC_Update(_ctx, data, len); ASSERT(result == 1); } - // c++17 void UpdateData(std::string_view str) { UpdateData(reinterpret_cast(str.data()), str.size()); } - void UpdateData(std::string const& str) { UpdateData(str.c_str()); } /* explicit overload to avoid using the container template */ - void UpdateData(char const* str) { UpdateData(reinterpret_cast(str), strlen(str)); } /* explicit overload to avoid using the container template */ + void UpdateData(std::string_view str) { UpdateData(reinterpret_cast(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 - void UpdateData(Container const& c) { UpdateData(advstd::data(c), advstd::size(c)); } + void UpdateData(Container const& c) { UpdateData(std::data(c), std::size(c)); } void Finalize() { @@ -139,15 +121,10 @@ namespace Impl Digest _digest = { }; }; } -} -namespace Trinity -{ -namespace Crypto +namespace Trinity::Crypto { using HMAC_SHA1 = Trinity::Impl::GenericHMAC; using HMAC_SHA256 = Trinity::Impl::GenericHMAC; } -} - #endif diff --git a/src/common/Utilities/advstd.h b/src/common/Utilities/advstd.h index 975fc2c85ad..a39e0baadc1 100644 --- a/src/common/Utilities/advstd.h +++ b/src/common/Utilities/advstd.h @@ -133,35 +133,6 @@ namespace advstd // C++20 std::remove_cvref_t template using remove_cvref_t = std::remove_cv_t>; - - template - struct negation : std::integral_constant { }; - - template - struct conjunction : std::true_type { }; - template - struct conjunction : B1 { }; - template - struct conjunction : std::conditional_t, B1> { }; - - template - struct disjunction : std::false_type { }; - template - struct disjunction : B1 { }; - template - struct disjunction : std::conditional_t> { }; - - template - constexpr T const& clamp(T const& val, T const& lo, T const& hi) - { - if (hi < val) - return hi; - - if (val < lo) - return lo; - - return val; - } } #endif -- cgit v1.2.3