Replace Hasher CRTP base with a concept.
Chaos Ci / test (push) Successful in 3m17s
Chaos Ci / benchmark (push) Successful in 1m54s

This commit is contained in:
hashlag
2026-08-31 01:22:07 +03:00
parent 2f1b5091cd
commit b03fddd40b
5 changed files with 18 additions and 37 deletions
+8 -31
View File
@@ -1,42 +1,19 @@
#ifndef CHAOS_HASH_HASHER_HPP #ifndef CHAOS_HASH_HASHER_HPP
#define CHAOS_HASH_HASHER_HPP #define CHAOS_HASH_HASHER_HPP
#include <cstdint>
#include "Hash.hpp"
namespace Chaos::Hash namespace Chaos::Hash
{ {
template<typename T> template<typename T>
class Hasher concept Hasher = requires(T hasher, uint8_t * begin, uint8_t * end)
{ {
public: hasher.Reset();
void Reset() hasher.Update(begin, end);
{ { hasher.Finish() } -> Hash;
Impl().Reset();
}
template<typename InputIt>
void Update(InputIt begin, InputIt end)
{
Impl().Update(begin, end);
}
auto Finish()
{
return Impl().Finish();
}
protected:
Hasher() = default;
private:
const T & Impl() const
{
return static_cast<const T &>(*this);
}
T & Impl()
{
return static_cast<T &>(*this);
}
}; };
} // namespace Chaos::Hash } // namespace Chaos::Hash
+3 -1
View File
@@ -163,7 +163,7 @@ struct Md4Hash
static_assert(Hash<Md4Hash>); static_assert(Hash<Md4Hash>);
class Md4Hasher : public Hasher<Md4Hasher> class Md4Hasher
{ {
public: public:
using HashType = Md4Hash; using HashType = Md4Hash;
@@ -304,6 +304,8 @@ private:
} }
}; };
static_assert(Hasher<Md4Hasher>);
} // namespace Chaos::Hash::Md4 } // namespace Chaos::Hash::Md4
#endif // CHAOS_HASH_MD4_HPP #endif // CHAOS_HASH_MD4_HPP
+3 -1
View File
@@ -187,7 +187,7 @@ struct Md5Hash
static_assert(Hash<Md5Hash>); static_assert(Hash<Md5Hash>);
class Md5Hasher : public Hasher<Md5Hasher> class Md5Hasher
{ {
public: public:
using HashType = Md5Hash; using HashType = Md5Hash;
@@ -328,6 +328,8 @@ private:
} }
}; };
static_assert(Hasher<Md5Hasher>);
} // namespace Chaos::Hash::Md5 } // namespace Chaos::Hash::Md5
#endif // CHAOS_HASH_MD5_HPP #endif // CHAOS_HASH_MD5_HPP
+3 -1
View File
@@ -157,7 +157,7 @@ struct Sha1Hash
static_assert(Hash<Sha1Hash>); static_assert(Hash<Sha1Hash>);
class Sha1Hasher : public Hasher<Sha1Hasher> class Sha1Hasher
{ {
public: public:
using HashType = Sha1Hash; using HashType = Sha1Hash;
@@ -298,6 +298,8 @@ private:
} }
}; };
static_assert(Hasher<Sha1Hasher>);
} // namespace Chaos::Hash::Sha1 } // namespace Chaos::Hash::Sha1
#endif // CHAOS_HASH_SHA1_HPP #endif // CHAOS_HASH_SHA1_HPP
+1 -3
View File
@@ -3,7 +3,6 @@
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <type_traits>
#include "Hash/Hasher.hpp" #include "Hash/Hasher.hpp"
#include "Service/ChaosException.hpp" #include "Service/ChaosException.hpp"
@@ -11,8 +10,7 @@
namespace Chaos::Mac::Hmac namespace Chaos::Mac::Hmac
{ {
template<typename HasherImpl, template<Hash::Hasher HasherImpl>
typename = std::enable_if_t<std::is_base_of_v<Hash::Hasher<HasherImpl>, HasherImpl>>>
class Hmac class Hmac
{ {
public: public: