summaryrefslogtreecommitdiff
path: root/src/buildtool/crypto/hasher.cpp
diff options
context:
space:
mode:
authorMaksim Denisov <denisov.maksim@huawei.com>2024-07-17 12:57:28 +0200
committerMaksim Denisov <denisov.maksim@huawei.com>2024-07-22 17:02:34 +0200
commitb2f51059cc034f03c70df28a5597a591ed3e5c5d (patch)
treefa607f601f623f70a05aabc18801c6daed4c2a18 /src/buildtool/crypto/hasher.cpp
parent0b80611163ffedb87dc2305320906f27e502cbcd (diff)
downloadjustbuild-b2f51059cc034f03c70df28a5597a591ed3e5c5d.tar.gz
Create Hasher using a static function
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();
+}