mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:27:45 +00:00
LibCrypto: Mutualize Digest
s
This commit is contained in:
parent
4e851145ba
commit
3f0e425f1e
5 changed files with 21 additions and 33 deletions
|
@ -13,11 +13,24 @@
|
||||||
namespace Crypto {
|
namespace Crypto {
|
||||||
namespace Hash {
|
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 {
|
class HashFunction {
|
||||||
public:
|
public:
|
||||||
|
static_assert(BlockS % 8 == 0);
|
||||||
static constexpr auto BlockSize = BlockS / 8;
|
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;
|
using DigestType = DigestT;
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ struct MultiHashDigestVariant {
|
||||||
DigestVariant m_digest {};
|
DigestVariant m_digest {};
|
||||||
};
|
};
|
||||||
|
|
||||||
class Manager final : public HashFunction<0, MultiHashDigestVariant> {
|
class Manager final : public HashFunction<0, 0, MultiHashDigestVariant> {
|
||||||
public:
|
public:
|
||||||
using HashFunction::update;
|
using HashFunction::update;
|
||||||
|
|
||||||
|
|
|
@ -13,14 +13,6 @@
|
||||||
namespace Crypto {
|
namespace Crypto {
|
||||||
namespace Hash {
|
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 {
|
namespace MD5Constants {
|
||||||
|
|
||||||
constexpr u32 init_A = 0x67452301;
|
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:
|
public:
|
||||||
using HashFunction::update;
|
using HashFunction::update;
|
||||||
|
|
||||||
|
|
|
@ -25,16 +25,7 @@ constexpr static u32 RoundConstants[4] {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<size_t Bytes>
|
class SHA1 final : public HashFunction<512, 160> {
|
||||||
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>> {
|
|
||||||
public:
|
public:
|
||||||
using HashFunction::update;
|
using HashFunction::update;
|
||||||
|
|
||||||
|
|
|
@ -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
|
// 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:
|
public:
|
||||||
using HashFunction::update;
|
using HashFunction::update;
|
||||||
|
|
||||||
|
@ -131,7 +123,7 @@ private:
|
||||||
constexpr static auto Rounds = 64;
|
constexpr static auto Rounds = 64;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SHA384 final : public HashFunction<1024, SHA2Digest<384 / 8>> {
|
class SHA384 final : public HashFunction<1024, 384> {
|
||||||
public:
|
public:
|
||||||
using HashFunction::update;
|
using HashFunction::update;
|
||||||
|
|
||||||
|
@ -181,7 +173,7 @@ private:
|
||||||
constexpr static auto Rounds = 80;
|
constexpr static auto Rounds = 80;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SHA512 final : public HashFunction<1024, SHA2Digest<512 / 8>> {
|
class SHA512 final : public HashFunction<1024, 512> {
|
||||||
public:
|
public:
|
||||||
using HashFunction::update;
|
using HashFunction::update;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue