Files
Library/include/Void/Crypto/AsyncHash.hpp
T

91 lines
2.7 KiB
C++

#pragma once
#include <Void/Async/Executor.hpp>
#include <Void/Async/Operation.hpp>
#include <Void/Async/Task.hpp>
#include <Void/Crypto/Crypto.hpp>
#include <Void/IO/Driver.hpp>
#include <Void/IO/Stream.hpp>
#include <Void/Time/Clock.hpp>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <memory>
namespace Void::CryptoAsync
{
struct StreamHashOptions {
std::size_t chunk_bytes{
Void::Crypto::CryptoLimits::kStreamChunkBytes};
Void::Crypto::ProviderOptions provider{};
};
/// Incrementally hash an IO::ByteStream using a Driver-owned BufferLease
/// (default 64 KiB). Checks stop/deadline between chunks and finalizes only
/// on clean EOF without cancellation.
[[nodiscard]] Void::Coro::Task<Void::Result::Result<Void::Crypto::Digest>>
hash_stream(Void::Crypto::HashAlgorithm algorithm,
Void::IO::Driver& driver, Void::IO::ByteStream& stream,
Void::Async::OperationOptions options = {},
StreamHashOptions hash_options = {});
struct FileHasherOptions {
std::size_t max_concurrent{
Void::Crypto::CryptoLimits::kDefaultMaxConcurrentFileHashes};
std::size_t max_waiting{
Void::Crypto::CryptoLimits::kDefaultMaxWaitingFileHashes};
std::size_t buffer_budget_bytes{
Void::Crypto::CryptoLimits::kDefaultAsyncBufferBudgetBytes};
std::size_t chunk_bytes{
Void::Crypto::CryptoLimits::kStreamChunkBytes};
Void::Crypto::ProviderOptions provider{};
};
struct FileHasherStats {
std::size_t active{0};
std::size_t waiting{0};
std::size_t reserved_buffer_bytes{0};
std::uint64_t completed{0};
std::uint64_t rejected{0};
bool closing{false};
};
/// Bounded asynchronous file-hashing service with concurrency, queue, and
/// owned buffer-budget limits plus explicit stop/join shutdown.
class AsyncFileHasher
{
public:
explicit AsyncFileHasher(
Void::Async::Executor& executor,
FileHasherOptions options = {},
const Void::Time::MonotonicClock& clock =
Void::Time::SystemMonotonicClock::instance());
~AsyncFileHasher();
AsyncFileHasher(const AsyncFileHasher&) = delete;
AsyncFileHasher& operator=(const AsyncFileHasher&) = delete;
AsyncFileHasher(AsyncFileHasher&&) = delete;
AsyncFileHasher& operator=(AsyncFileHasher&&) = delete;
[[nodiscard]] Void::Coro::Task<
Void::Result::Result<Void::Crypto::Digest>>
hash_file(Void::Crypto::HashAlgorithm algorithm,
std::filesystem::path path,
Void::Async::OperationOptions options = {});
void request_stop() noexcept;
[[nodiscard]] Void::Result::Result<void> close_and_join();
[[nodiscard]] FileHasherStats stats() const noexcept;
private:
struct State;
std::shared_ptr<State> m_state;
};
} // namespace Void::CryptoAsync