diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2024-04-22 13:23:30 +0200 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2024-04-22 14:48:07 +0200 |
commit | f7296f50103dc9320ffa1127640c843b227adfdd (patch) | |
tree | 125dac9f76148573b8fea25875bde34d1c97a86d | |
parent | e79f95584bab08191c0b765ad6481308a7ef36a3 (diff) | |
download | justbuild-f7296f50103dc9320ffa1127640c843b227adfdd.tar.gz |
Compactification: Obtain hash string length from hasher.
-rw-r--r-- | src/buildtool/crypto/hash_impl_sha1.cpp | 4 | ||||
-rw-r--r-- | src/buildtool/crypto/hash_impl_sha256.cpp | 4 | ||||
-rw-r--r-- | src/buildtool/crypto/hash_impl_sha512.cpp | 4 | ||||
-rw-r--r-- | src/buildtool/crypto/hasher.hpp | 13 |
4 files changed, 25 insertions, 0 deletions
diff --git a/src/buildtool/crypto/hash_impl_sha1.cpp b/src/buildtool/crypto/hash_impl_sha1.cpp index 97ff5464..d188f9cf 100644 --- a/src/buildtool/crypto/hash_impl_sha1.cpp +++ b/src/buildtool/crypto/hash_impl_sha1.cpp @@ -51,6 +51,10 @@ class HashImplSha1 final : public Hasher::IHashImpl { return {}; } + [[nodiscard]] auto GetHashLength() const noexcept -> size_t final { + return SHA_DIGEST_LENGTH * kCharsPerNumber; + } + private: SHA_CTX ctx_{}; bool initialized_{}; diff --git a/src/buildtool/crypto/hash_impl_sha256.cpp b/src/buildtool/crypto/hash_impl_sha256.cpp index ea96a3ba..a677807b 100644 --- a/src/buildtool/crypto/hash_impl_sha256.cpp +++ b/src/buildtool/crypto/hash_impl_sha256.cpp @@ -51,6 +51,10 @@ class HashImplSha256 final : public Hasher::IHashImpl { return {}; } + [[nodiscard]] auto GetHashLength() const noexcept -> size_t final { + return SHA256_DIGEST_LENGTH * kCharsPerNumber; + } + private: SHA256_CTX ctx_{}; bool initialized_{}; diff --git a/src/buildtool/crypto/hash_impl_sha512.cpp b/src/buildtool/crypto/hash_impl_sha512.cpp index 4b96e470..41ca792b 100644 --- a/src/buildtool/crypto/hash_impl_sha512.cpp +++ b/src/buildtool/crypto/hash_impl_sha512.cpp @@ -51,6 +51,10 @@ class HashImplSha512 final : public Hasher::IHashImpl { return {}; } + [[nodiscard]] auto GetHashLength() const noexcept -> size_t final { + return SHA512_DIGEST_LENGTH * kCharsPerNumber; + } + private: SHA512_CTX ctx_{}; bool initialized_{}; diff --git a/src/buildtool/crypto/hasher.hpp b/src/buildtool/crypto/hasher.hpp index 4bbcc7b7..a926fab1 100644 --- a/src/buildtool/crypto/hasher.hpp +++ b/src/buildtool/crypto/hasher.hpp @@ -17,6 +17,7 @@ #include <cstddef> #include <cstdint> +#include <limits> #include <memory> #include <optional> #include <string> @@ -69,6 +70,10 @@ class Hasher { /// \brief Interface for hash implementations class IHashImpl { public: + static constexpr size_t kCharsPerNumber = + std::numeric_limits<std::uint8_t>::max() / + std::numeric_limits<signed char>::max(); + IHashImpl() noexcept = default; IHashImpl(IHashImpl const&) = delete; IHashImpl(IHashImpl&&) = default; @@ -89,6 +94,9 @@ class Hasher { [[nodiscard]] virtual auto Compute(std::string const& data) && noexcept -> std::string = 0; + /// \brief Obtain length of the resulting hash string. + [[nodiscard]] virtual auto GetHashLength() const noexcept -> size_t = 0; + static auto FatalError() noexcept -> void { Logger::Log(LogLevel::Error, "Failed to compute hash."); std::terminate(); @@ -111,6 +119,11 @@ class Hasher { return HashDigest{{}}; } + /// \brief Obtain length of the resulting hash string. + [[nodiscard]] auto GetHashLength() const noexcept -> size_t { + return impl_->GetHashLength(); + } + private: std::unique_ptr<IHashImpl> impl_; |