diff options
author | Michel Hermier <michel.hermier@gmail.com> | 2022-01-03 22:28:19 +0100 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-01-05 01:07:21 +0330 |
commit | 3f0e425f1eaf4ea03d3e7fddf367f8532c2e43b4 (patch) | |
tree | 7afa99aaf43703f611fe19a56f9004c0e681c061 | |
parent | 4e851145baca6cabb02c13c36e19c8dd3ef61003 (diff) | |
download | serenity-3f0e425f1eaf4ea03d3e7fddf367f8532c2e43b4.zip |
LibCrypto: Mutualize `Digest`s
-rw-r--r-- | Userland/Libraries/LibCrypto/Hash/HashFunction.h | 17 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/Hash/HashManager.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/Hash/MD5.h | 10 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/Hash/SHA1.h | 11 | ||||
-rw-r--r-- | Userland/Libraries/LibCrypto/Hash/SHA2.h | 14 |
5 files changed, 21 insertions, 33 deletions
diff --git a/Userland/Libraries/LibCrypto/Hash/HashFunction.h b/Userland/Libraries/LibCrypto/Hash/HashFunction.h index a766aa8540..59d9b83cf9 100644 --- a/Userland/Libraries/LibCrypto/Hash/HashFunction.h +++ b/Userland/Libraries/LibCrypto/Hash/HashFunction.h @@ -13,11 +13,24 @@ namespace Crypto { namespace Hash { -template<size_t BlockS, typename DigestT> +template<size_t DigestS> +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<size_t BlockS, size_t DigestS, typename DigestT = Digest<DigestS>> 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 b161ae87d6..a065274c46 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 9f07c6b899..6e91276953 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 a4832785dc..8b3f574390 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<size_t Bytes> -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 b9377e2c48..5421b2bdf0 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<size_t Bytes> -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<size_t BlockSize> 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; |