summaryrefslogtreecommitdiff
path: root/src/buildtool/crypto/hasher.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/buildtool/crypto/hasher.cpp')
-rw-r--r--src/buildtool/crypto/hasher.cpp42
1 files changed, 37 insertions, 5 deletions
diff --git a/src/buildtool/crypto/hasher.cpp b/src/buildtool/crypto/hasher.cpp
index 60a029c2..6e2f6ee3 100644
--- a/src/buildtool/crypto/hasher.cpp
+++ b/src/buildtool/crypto/hasher.cpp
@@ -17,16 +17,48 @@
#include "src/buildtool/crypto/hash_impl_sha1.hpp"
#include "src/buildtool/crypto/hash_impl_sha256.hpp"
#include "src/buildtool/crypto/hash_impl_sha512.hpp"
+#include "src/buildtool/logging/log_level.hpp"
+#include "src/buildtool/logging/logger.hpp"
-auto Hasher::CreateHashImpl(HashType type) noexcept
- -> std::unique_ptr<IHashImpl> {
+namespace {
+[[nodiscard]] auto CreateHashImpl(Hasher::HashType type) noexcept
+ -> std::unique_ptr<Hasher::IHashImpl> {
switch (type) {
- case HashType::SHA1:
+ case Hasher::HashType::SHA1:
return CreateHashImplSha1();
- case HashType::SHA256:
+ case Hasher::HashType::SHA256:
return CreateHashImplSha256();
- case HashType::SHA512:
+ case Hasher::HashType::SHA512:
return CreateHashImplSha512();
}
return nullptr; // make gcc happy
}
+} // namespace
+
+Hasher::Hasher(std::unique_ptr<IHashImpl> impl) noexcept
+ : impl_{std::move(impl)} {}
+
+auto Hasher::Create(HashType type) noexcept -> std::optional<Hasher> {
+ auto impl = CreateHashImpl(type);
+ if (impl == nullptr) {
+ return std::nullopt;
+ }
+ return Hasher{std::move(impl)};
+}
+
+auto Hasher::Update(std::string const& data) noexcept -> bool {
+ return impl_->Update(data);
+}
+
+auto Hasher::Finalize() && noexcept -> HashDigest {
+ auto hash = std::move(*impl_).Finalize();
+ if (not hash) {
+ Logger::Log(LogLevel::Error, "Failed to compute hash.");
+ std::terminate();
+ }
+ return HashDigest{std::move(*hash)};
+}
+
+auto Hasher::GetHashLength() const noexcept -> std::size_t {
+ return impl_->GetHashLength();
+}