diff --git a/Userland/Libraries/LibCrypto/Hash/HashFunction.h b/Userland/Libraries/LibCrypto/Hash/HashFunction.h index a766aa85400..59d9b83cf93 100644 --- a/Userland/Libraries/LibCrypto/Hash/HashFunction.h +++ b/Userland/Libraries/LibCrypto/Hash/HashFunction.h @@ -13,11 +13,24 @@ namespace Crypto { namespace Hash { -template +template +struct Digest { + static_assert(DigestS % 8 == 0); + constexpr static size_t Size = DigestS / 8; + u8 data[Size]; + + [[nodiscard]] ALWAYS_INLINE const u8* immutable_data() const { return data; } + [[nodiscard]] ALWAYS_INLINE size_t data_length() const { return Size; } +}; + +template> class HashFunction { public: + static_assert(BlockS % 8 == 0); static constexpr auto BlockSize = BlockS / 8; - static constexpr auto DigestSize = DigestT::Size; + + static_assert(DigestS % 8 == 0); + static constexpr auto DigestSize = DigestS / 8; using DigestType = DigestT; diff --git a/Userland/Libraries/LibCrypto/Hash/HashManager.h b/Userland/Libraries/LibCrypto/Hash/HashManager.h index b161ae87d6a..a065274c46e 100644 --- a/Userland/Libraries/LibCrypto/Hash/HashManager.h +++ b/Userland/Libraries/LibCrypto/Hash/HashManager.h @@ -77,7 +77,7 @@ struct MultiHashDigestVariant { DigestVariant m_digest {}; }; -class Manager final : public HashFunction<0, MultiHashDigestVariant> { +class Manager final : public HashFunction<0, 0, MultiHashDigestVariant> { public: using HashFunction::update; diff --git a/Userland/Libraries/LibCrypto/Hash/MD5.h b/Userland/Libraries/LibCrypto/Hash/MD5.h index 9f07c6b899f..6e912769538 100644 --- a/Userland/Libraries/LibCrypto/Hash/MD5.h +++ b/Userland/Libraries/LibCrypto/Hash/MD5.h @@ -13,14 +13,6 @@ namespace Crypto { namespace Hash { -struct MD5Digest { - constexpr static size_t Size = 16; - u8 data[Size]; - - const u8* immutable_data() const { return data; } - size_t data_length() const { return Size; } -}; - namespace MD5Constants { constexpr u32 init_A = 0x67452301; @@ -53,7 +45,7 @@ constexpr u8 PADDING[] = { } -class MD5 final : public HashFunction<512, MD5Digest> { +class MD5 final : public HashFunction<512, 128> { public: using HashFunction::update; diff --git a/Userland/Libraries/LibCrypto/Hash/SHA1.h b/Userland/Libraries/LibCrypto/Hash/SHA1.h index a4832785dc0..8b3f5743900 100644 --- a/Userland/Libraries/LibCrypto/Hash/SHA1.h +++ b/Userland/Libraries/LibCrypto/Hash/SHA1.h @@ -25,16 +25,7 @@ constexpr static u32 RoundConstants[4] { } -template -struct SHA1Digest { - u8 data[Bytes]; - constexpr static size_t Size = Bytes; - - const u8* immutable_data() const { return data; } - size_t data_length() const { return Bytes; } -}; - -class SHA1 final : public HashFunction<512, SHA1Digest<160 / 8>> { +class SHA1 final : public HashFunction<512, 160> { public: using HashFunction::update; diff --git a/Userland/Libraries/LibCrypto/Hash/SHA2.h b/Userland/Libraries/LibCrypto/Hash/SHA2.h index b9377e2c482..5421b2bdf03 100644 --- a/Userland/Libraries/LibCrypto/Hash/SHA2.h +++ b/Userland/Libraries/LibCrypto/Hash/SHA2.h @@ -72,16 +72,8 @@ constexpr static u64 InitializationHashes[8] = { }; } -template -struct SHA2Digest { - u8 data[Bytes]; - constexpr static size_t Size = Bytes; - const u8* immutable_data() const { return data; } - size_t data_length() const { return Bytes; } -}; - // FIXME: I want template but the compiler gets confused -class SHA256 final : public HashFunction<512, SHA2Digest<256 / 8>> { +class SHA256 final : public HashFunction<512, 256> { public: using HashFunction::update; @@ -131,7 +123,7 @@ private: constexpr static auto Rounds = 64; }; -class SHA384 final : public HashFunction<1024, SHA2Digest<384 / 8>> { +class SHA384 final : public HashFunction<1024, 384> { public: using HashFunction::update; @@ -181,7 +173,7 @@ private: constexpr static auto Rounds = 80; }; -class SHA512 final : public HashFunction<1024, SHA2Digest<512 / 8>> { +class SHA512 final : public HashFunction<1024, 512> { public: using HashFunction::update;