diff --git a/include/tscore/CryptoHash.h b/include/tscore/CryptoHash.h index 02c1f9d7147..c486bacc4f1 100644 --- a/include/tscore/CryptoHash.h +++ b/include/tscore/CryptoHash.h @@ -111,53 +111,42 @@ union CryptoHash { extern CryptoHash const CRYPTO_HASH_ZERO; -/** Protocol class for a crypto hash context. - - A hash of this type is used for strong hashing, such as for URLs. -*/ -class CryptoContextBase +class CryptoContext { - typedef CryptoContextBase self; ///< Self reference type. public: - /// Destructor (force virtual) - virtual ~CryptoContextBase() {} - /// Update the hash with @a data of @a length bytes. - virtual bool update(void const *data, int length) = 0; - /// Finalize and extract the @a hash. - virtual bool finalize(CryptoHash &hash) = 0; + /** Protocol class for a crypto hash context. - /// Convenience overload. - bool finalize(CryptoHash *hash); + A hash of this type is used for strong hashing, such as for URLs. + */ + class Hasher + { + using self_type = Hasher; ///< Self reference type. + public: + /// Destructor (force virtual) + virtual ~Hasher() {} + /// Update the hash with @a data of @a length bytes. + virtual bool update(void const *data, int length) = 0; + /// Finalize and extract the @a hash. + virtual bool finalize(CryptoHash &hash) = 0; + + /// Convenience overload. + bool finalize(CryptoHash *hash); + + protected: + EVP_MD_CTX *_ctx = nullptr; + }; + + CryptoContext(); + /// Update the hash with @a data of @a length bytes. + bool update(void const *data, int length); /// Convenience - compute final @a hash for @a data. /// @note This is just as fast as the previous style, as a new context must be initialized /// every time this is done. bool hash_immediate(CryptoHash &hash, void const *data, int length); -protected: - EVP_MD_CTX *_ctx = nullptr; -}; - -inline bool -CryptoContextBase::hash_immediate(CryptoHash &hash, void const *data, int length) -{ - return this->update(data, length) && this->finalize(hash); -} - -inline bool -CryptoContextBase::finalize(CryptoHash *hash) -{ - return this->finalize(*hash); -} - -class CryptoContext : public CryptoContextBase -{ -public: - CryptoContext(); - /// Update the hash with @a data of @a length bytes. - bool update(void const *data, int length) override; /// Finalize and extract the @a hash. - bool finalize(CryptoHash &hash) override; + bool finalize(CryptoHash &hash); enum HashType { UNSPECIFIED, @@ -168,23 +157,40 @@ class CryptoContext : public CryptoContextBase }; ///< What type of hash we really are. static HashType Setting; - ~CryptoContext() { reinterpret_cast(_base)->~CryptoContextBase(); } + ~CryptoContext(); private: static size_t constexpr OBJ_SIZE = 256; char _base[OBJ_SIZE]; }; +inline bool +CryptoContext::Hasher::finalize(CryptoHash *hash) +{ + return this->finalize(*hash); +} + inline bool CryptoContext::update(void const *data, int length) { - return reinterpret_cast(_base)->update(data, length); + return reinterpret_cast(_base)->update(data, length); } inline bool CryptoContext::finalize(CryptoHash &hash) { - return reinterpret_cast(_base)->finalize(hash); + return reinterpret_cast(_base)->finalize(hash); +} + +inline bool +CryptoContext::hash_immediate(CryptoHash &hash, void const *data, int length) +{ + return this->update(data, length) && this->finalize(hash); +} + +inline CryptoContext::~CryptoContext() +{ + std::destroy_at(reinterpret_cast(_base)); } ts::BufferWriter &bwformat(ts::BufferWriter &w, ts::BWFSpec const &spec, ats::CryptoHash const &hash); diff --git a/include/tscore/MD5.h b/include/tscore/MD5.h index b26a2e1ca72..67171f7dd16 100644 --- a/include/tscore/MD5.h +++ b/include/tscore/MD5.h @@ -31,7 +31,7 @@ #include #endif -class MD5Context : public ats::CryptoContextBase +class MD5Context : public ats::CryptoContext::Hasher { public: MD5Context() @@ -90,4 +90,4 @@ class MD5Context : public ats::CryptoContextBase #endif }; -typedef CryptoHash INK_MD5; +using INK_MD5 = CryptoHash; diff --git a/include/tscore/MMH.h b/include/tscore/MMH.h index 15d74b64254..efa52094bcd 100644 --- a/include/tscore/MMH.h +++ b/include/tscore/MMH.h @@ -50,7 +50,7 @@ int ink_code_MMH(unsigned char *input, int len, unsigned char *sixteen_byte_hash cost. */ -class MMHContext : public ats::CryptoContextBase +class MMHContext : public ats::CryptoContext::Hasher { protected: MMH_CTX _ctx; diff --git a/include/tscore/SHA256.h b/include/tscore/SHA256.h index 268908d9b40..a22345d9afa 100644 --- a/include/tscore/SHA256.h +++ b/include/tscore/SHA256.h @@ -31,7 +31,7 @@ #include #endif -class SHA256Context : public ats::CryptoContextBase +class SHA256Context : public ats::CryptoContext::Hasher { public: SHA256Context()