Crypto: Hasher state export/import for the built-in backend (closes #53)

This commit is contained in:
faumaray committed 2026-12-04 00:27:26 +03:00
1 parent 217b20e37a
commit 7d6834979e
5 files changed
+325

No files matched your search

+58
View File
@@ -613,6 +613,64 @@ namespace Void::Crypto
#endif
}
Void::Result::Result<std::vector<std::byte>> Hasher::export_state() const
{
if (!m_impl) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::invalid_state,
"Crypto::Hasher::export_state: hasher is moved-from")};
}
if (m_impl->state != HasherState::Ready &&
m_impl->state != HasherState::Updating) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::invalid_state,
"Crypto::Hasher::export_state: hasher is not in progress")};
}
if (!m_impl->using_builtin) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Hasher::export_state: state export requires the "
"built-in backend")};
}
std::vector<std::byte> state;
if (!m_impl->builtin.export_state(state)) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::backend_error,
"Crypto::Hasher::export_state: state export failed")};
}
return state;
}
Void::Result::Result<void>
Hasher::import_state(std::span<const std::byte> state)
{
if (!m_impl) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::invalid_state,
"Crypto::Hasher::import_state: hasher is moved-from")};
}
if (m_impl->state != HasherState::Ready &&
m_impl->state != HasherState::Updating) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::invalid_state,
"Crypto::Hasher::import_state: hasher is not in progress")};
}
if (!m_impl->using_builtin) {
return std::unexpected{make_crypto_error(
CryptoErrorCode::unsupported_algorithm,
"Crypto::Hasher::import_state: state import requires the "
"built-in backend")};
}
if (!m_impl->builtin.import_state(state)) {
m_impl->state = HasherState::Failed;
return std::unexpected{make_crypto_error(
CryptoErrorCode::invalid_argument,
"Crypto::Hasher::import_state: malformed or incompatible "
"state")};
}
return {};
}
auto hash(HashAlgorithm algorithm, std::span<const std::byte> data)
-> Void::Result::Result<Digest>
{