mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:07:45 +00:00
LibCrypto: Use AK::Variant in MultiHashDigestVariant
This commit is contained in:
parent
2c1916dd8d
commit
73f585ceb4
4 changed files with 25 additions and 56 deletions
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include <AK/Optional.h>
|
#include <AK/Optional.h>
|
||||||
#include <AK/OwnPtr.h>
|
#include <AK/OwnPtr.h>
|
||||||
|
#include <AK/Variant.h>
|
||||||
#include <LibCrypto/Hash/HashFunction.h>
|
#include <LibCrypto/Hash/HashFunction.h>
|
||||||
#include <LibCrypto/Hash/MD5.h>
|
#include <LibCrypto/Hash/MD5.h>
|
||||||
#include <LibCrypto/Hash/SHA1.h>
|
#include <LibCrypto/Hash/SHA1.h>
|
||||||
|
@ -26,85 +27,53 @@ enum class HashKind {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MultiHashDigestVariant {
|
struct MultiHashDigestVariant {
|
||||||
|
|
||||||
constexpr static size_t Size = 0;
|
constexpr static size_t Size = 0;
|
||||||
|
|
||||||
|
MultiHashDigestVariant(MD5::DigestType digest)
|
||||||
|
: m_digest(move(digest))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
MultiHashDigestVariant(SHA1::DigestType digest)
|
MultiHashDigestVariant(SHA1::DigestType digest)
|
||||||
: sha1(digest)
|
: m_digest(move(digest))
|
||||||
, kind(HashKind::SHA1)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
MultiHashDigestVariant(SHA256::DigestType digest)
|
MultiHashDigestVariant(SHA256::DigestType digest)
|
||||||
: sha256(digest)
|
: m_digest(move(digest))
|
||||||
, kind(HashKind::SHA256)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
MultiHashDigestVariant(SHA384::DigestType digest)
|
MultiHashDigestVariant(SHA384::DigestType digest)
|
||||||
: sha384(digest)
|
: m_digest(move(digest))
|
||||||
, kind(HashKind::SHA384)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
MultiHashDigestVariant(SHA512::DigestType digest)
|
MultiHashDigestVariant(SHA512::DigestType digest)
|
||||||
: sha512(digest)
|
: m_digest(move(digest))
|
||||||
, kind(HashKind::SHA512)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
MultiHashDigestVariant(MD5::DigestType digest)
|
|
||||||
: md5(digest)
|
|
||||||
, kind(HashKind::MD5)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
const u8* immutable_data() const
|
const u8* immutable_data() const
|
||||||
{
|
{
|
||||||
switch (kind) {
|
const u8* data = nullptr;
|
||||||
case HashKind::MD5:
|
m_digest.visit(
|
||||||
return md5.value().immutable_data();
|
[&](const Empty&) { VERIFY_NOT_REACHED(); },
|
||||||
case HashKind::SHA1:
|
[&](const auto& value) { data = value.immutable_data(); });
|
||||||
return sha1.value().immutable_data();
|
return data;
|
||||||
case HashKind::SHA256:
|
|
||||||
return sha256.value().immutable_data();
|
|
||||||
case HashKind::SHA384:
|
|
||||||
return sha384.value().immutable_data();
|
|
||||||
case HashKind::SHA512:
|
|
||||||
return sha512.value().immutable_data();
|
|
||||||
default:
|
|
||||||
case HashKind::None:
|
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t data_length()
|
size_t data_length()
|
||||||
{
|
{
|
||||||
switch (kind) {
|
size_t length = 0;
|
||||||
case HashKind::MD5:
|
m_digest.visit(
|
||||||
return md5.value().data_length();
|
[&](const Empty&) { VERIFY_NOT_REACHED(); },
|
||||||
case HashKind::SHA1:
|
[&](const auto& value) { length = value.data_length(); });
|
||||||
return sha1.value().data_length();
|
return length;
|
||||||
case HashKind::SHA256:
|
|
||||||
return sha256.value().data_length();
|
|
||||||
case HashKind::SHA384:
|
|
||||||
return sha384.value().data_length();
|
|
||||||
case HashKind::SHA512:
|
|
||||||
return sha512.value().data_length();
|
|
||||||
default:
|
|
||||||
case HashKind::None:
|
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<SHA1::DigestType> sha1;
|
using DigestVariant = Variant<Empty, MD5::DigestType, SHA1::DigestType, SHA256::DigestType, SHA384::DigestType, SHA512::DigestType>;
|
||||||
Optional<SHA256::DigestType> sha256;
|
DigestVariant m_digest { Empty {} };
|
||||||
Optional<SHA384::DigestType> sha384;
|
|
||||||
Optional<SHA512::DigestType> sha512;
|
|
||||||
Optional<MD5::DigestType> md5;
|
|
||||||
HashKind kind { HashKind::None };
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Manager final : public HashFunction<0, MultiHashDigestVariant> {
|
class Manager final : public HashFunction<0, MultiHashDigestVariant> {
|
||||||
|
|
|
@ -18,7 +18,7 @@ struct MD5Digest {
|
||||||
u8 data[Size];
|
u8 data[Size];
|
||||||
|
|
||||||
const u8* immutable_data() const { return data; }
|
const u8* immutable_data() const { return data; }
|
||||||
size_t data_length() { return Size; }
|
size_t data_length() const { return Size; }
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace MD5Constants {
|
namespace MD5Constants {
|
||||||
|
|
|
@ -31,7 +31,7 @@ struct SHA1Digest {
|
||||||
constexpr static size_t Size = Bytes;
|
constexpr static size_t Size = Bytes;
|
||||||
|
|
||||||
const u8* immutable_data() const { return data; }
|
const u8* immutable_data() const { return data; }
|
||||||
size_t data_length() { return Bytes; }
|
size_t data_length() const { return Bytes; }
|
||||||
};
|
};
|
||||||
|
|
||||||
class SHA1 final : public HashFunction<512, SHA1Digest<160 / 8>> {
|
class SHA1 final : public HashFunction<512, SHA1Digest<160 / 8>> {
|
||||||
|
|
|
@ -77,7 +77,7 @@ struct SHA2Digest {
|
||||||
u8 data[Bytes];
|
u8 data[Bytes];
|
||||||
constexpr static size_t Size = Bytes;
|
constexpr static size_t Size = Bytes;
|
||||||
const u8* immutable_data() const { return data; }
|
const u8* immutable_data() const { return data; }
|
||||||
size_t data_length() { return Bytes; }
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue