mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:27:45 +00:00
LibCrypto: Make Digest
s able to return bytes
This commit is contained in:
parent
3f0e425f1e
commit
22efc34ec5
3 changed files with 10 additions and 2 deletions
|
@ -21,6 +21,8 @@ struct Digest {
|
||||||
|
|
||||||
[[nodiscard]] ALWAYS_INLINE const u8* immutable_data() const { return data; }
|
[[nodiscard]] ALWAYS_INLINE const u8* immutable_data() const { return data; }
|
||||||
[[nodiscard]] ALWAYS_INLINE size_t data_length() const { return Size; }
|
[[nodiscard]] ALWAYS_INLINE size_t data_length() const { return Size; }
|
||||||
|
|
||||||
|
[[nodiscard]] ALWAYS_INLINE ReadonlyBytes bytes() const { return { immutable_data(), data_length() }; }
|
||||||
};
|
};
|
||||||
|
|
||||||
template<size_t BlockS, size_t DigestS, typename DigestT = Digest<DigestS>>
|
template<size_t BlockS, size_t DigestS, typename DigestT = Digest<DigestS>>
|
||||||
|
|
|
@ -73,6 +73,13 @@ struct MultiHashDigestVariant {
|
||||||
[&](const auto& value) { return value.data_length(); });
|
[&](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<Empty, MD5::DigestType, SHA1::DigestType, SHA256::DigestType, SHA384::DigestType, SHA512::DigestType>;
|
using DigestVariant = Variant<Empty, MD5::DigestType, SHA1::DigestType, SHA256::DigestType, SHA384::DigestType, SHA512::DigestType>;
|
||||||
DigestVariant m_digest {};
|
DigestVariant m_digest {};
|
||||||
};
|
};
|
||||||
|
|
|
@ -64,8 +64,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
|
|
||||||
while (!file->eof() && !file->has_error())
|
while (!file->eof() && !file->has_error())
|
||||||
hash.update(file->read(PAGE_SIZE));
|
hash.update(file->read(PAGE_SIZE));
|
||||||
auto digest = hash.digest();
|
outln("{:hex-dump} {}", hash.digest().bytes(), path);
|
||||||
outln("{:hex-dump} {}", ReadonlyBytes(digest.immutable_data(), digest.data_length()), path);
|
|
||||||
}
|
}
|
||||||
return has_error ? 1 : 0;
|
return has_error ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue