From a5be6de770c9152f3248ce0f01ac3645e30a19ed Mon Sep 17 00:00:00 2001 From: Maksim Denisov Date: Wed, 17 Jul 2024 16:26:25 +0200 Subject: Use compile-time polymorphism in Hasher ...since runtime polymorphism was there just to avoid including openssl as a public dependency. A combination of forward declarations, std::variant and std::unique_ptr is used instead. --- src/buildtool/crypto/hasher.hpp | 46 ++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) (limited to 'src/buildtool/crypto/hasher.hpp') 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 #include -#include #include #include #include @@ -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::max() / - std::numeric_limits::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 = 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&& 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 impl_; + std::unique_ptr sha_ctx_; - explicit Hasher(std::unique_ptr impl) noexcept; + explicit Hasher(std::unique_ptr sha_ctx) noexcept; }; #endif // INCLUDED_SRC_BUILDTOOL_CRYPTO_HASHER_HPP -- cgit v1.2.3