From 22efc34ec5ea171b79b351eee7bc59d309dc837e Mon Sep 17 00:00:00 2001 From: Michel Hermier Date: Tue, 4 Jan 2022 02:34:00 +0100 Subject: [PATCH] LibCrypto: Make `Digest`s able to return `bytes` --- Userland/Libraries/LibCrypto/Hash/HashFunction.h | 2 ++ Userland/Libraries/LibCrypto/Hash/HashManager.h | 7 +++++++ Userland/Utilities/checksum.cpp | 3 +-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibCrypto/Hash/HashFunction.h b/Userland/Libraries/LibCrypto/Hash/HashFunction.h index 59d9b83cf9..a803b1650b 100644 --- a/Userland/Libraries/LibCrypto/Hash/HashFunction.h +++ b/Userland/Libraries/LibCrypto/Hash/HashFunction.h @@ -21,6 +21,8 @@ struct Digest { [[nodiscard]] ALWAYS_INLINE const u8* immutable_data() const { return data; } [[nodiscard]] ALWAYS_INLINE size_t data_length() const { return Size; } + + [[nodiscard]] ALWAYS_INLINE ReadonlyBytes bytes() const { return { immutable_data(), data_length() }; } }; template> diff --git a/Userland/Libraries/LibCrypto/Hash/HashManager.h b/Userland/Libraries/LibCrypto/Hash/HashManager.h index a065274c46..4c31ecdf4a 100644 --- a/Userland/Libraries/LibCrypto/Hash/HashManager.h +++ b/Userland/Libraries/LibCrypto/Hash/HashManager.h @@ -73,6 +73,13 @@ struct MultiHashDigestVariant { [&](const auto& value) { return value.data_length(); }); } + [[nodiscard]] ReadonlyBytes bytes() const + { + return m_digest.visit( + [&](const Empty&) -> ReadonlyBytes { VERIFY_NOT_REACHED(); }, + [&](const auto& value) { return value.bytes(); }); + } + using DigestVariant = Variant; DigestVariant m_digest {}; }; diff --git a/Userland/Utilities/checksum.cpp b/Userland/Utilities/checksum.cpp index a91a77019f..6325898b8c 100644 --- a/Userland/Utilities/checksum.cpp +++ b/Userland/Utilities/checksum.cpp @@ -64,8 +64,7 @@ ErrorOr serenity_main(Main::Arguments arguments) while (!file->eof() && !file->has_error()) hash.update(file->read(PAGE_SIZE)); - auto digest = hash.digest(); - outln("{:hex-dump} {}", ReadonlyBytes(digest.immutable_data(), digest.data_length()), path); + outln("{:hex-dump} {}", hash.digest().bytes(), path); } return has_error ? 1 : 0; }