diff options
Diffstat (limited to 'src/buildtool/crypto/hasher.hpp')
-rw-r--r-- | src/buildtool/crypto/hasher.hpp | 46 |
1 files changed, 15 insertions, 31 deletions
diff --git a/src/buildtool/crypto/hasher.hpp b/src/buildtool/crypto/hasher.hpp index c37cd34e..4987f7bb 100644 --- a/src/buildtool/crypto/hasher.hpp +++ b/src/buildtool/crypto/hasher.hpp @@ -17,7 +17,6 @@ #include <cstddef> #include <cstdint> -#include <limits> #include <memory> #include <optional> #include <string> @@ -31,6 +30,8 @@ class Hasher final { /// \brief Types of hash implementations supported by generator. enum class HashType : std::uint8_t { SHA1, SHA256, SHA512 }; + struct ShaContext; + /// \brief The universal hash digest. /// The type of hash and the digest length depends on the hash /// implementation used to generated this digest. @@ -40,11 +41,11 @@ class Hasher final { public: /// \brief Get pointer to raw bytes of digest. /// Length can be obtained using \ref Length. - [[nodiscard]] auto Bytes() const& -> std::string const& { + [[nodiscard]] auto Bytes() const& noexcept -> std::string const& { return bytes_; } - [[nodiscard]] auto Bytes() && -> std::string { + [[nodiscard]] auto Bytes() && noexcept -> std::string { return std::move(bytes_); } @@ -65,35 +66,18 @@ class Hasher final { explicit HashDigest(std::string bytes) : bytes_{std::move(bytes)} {} }; - /// \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; - auto operator=(IHashImpl const&) -> IHashImpl& = delete; - auto operator=(IHashImpl&&) -> IHashImpl& = default; - virtual ~IHashImpl() = default; - - /// \brief Feed data to the incremental hashing. - [[nodiscard]] virtual auto Update(std::string const& data) noexcept - -> bool = 0; - - /// \brief Finalize the hashing and return hash as string of raw bytes. - [[nodiscard]] virtual auto Finalize() && noexcept - -> std::optional<std::string> = 0; - - /// \brief Obtain length of the resulting hash string. - [[nodiscard]] virtual auto GetHashLength() const noexcept -> size_t = 0; - }; - + /// \brief Create and initialize a hasher + /// \return An initialized hasher on success or std::nullopt on failure. [[nodiscard]] static auto Create(HashType type) noexcept -> std::optional<Hasher>; + Hasher(Hasher&& other) noexcept; + auto operator=(Hasher&& other) noexcept -> Hasher&; + + Hasher(Hasher const& other) noexcept = delete; + auto operator=(Hasher const& other) noexcept -> Hasher& = delete; + ~Hasher() noexcept; + /// \brief Feed data to the hasher. auto Update(std::string const& data) noexcept -> bool; @@ -104,9 +88,9 @@ class Hasher final { [[nodiscard]] auto GetHashLength() const noexcept -> std::size_t; private: - std::unique_ptr<IHashImpl> impl_; + std::unique_ptr<ShaContext> sha_ctx_; - explicit Hasher(std::unique_ptr<IHashImpl> impl) noexcept; + explicit Hasher(std::unique_ptr<ShaContext> sha_ctx) noexcept; }; #endif // INCLUDED_SRC_BUILDTOOL_CRYPTO_HASHER_HPP |