From 619def44c1cca9f3cdf63544d5f24f2c7a7d9b77 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Tue, 22 Feb 2022 17:03:21 +0100 Subject: Initial self-hosting commit This is the initial version of our tool that is able to build itself. In can be bootstrapped by ./bin/bootstrap.py Co-authored-by: Oliver Reiche Co-authored-by: Victor Moreno --- src/buildtool/crypto/hash_impl_sha256.cpp | 50 +++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/buildtool/crypto/hash_impl_sha256.cpp (limited to 'src/buildtool/crypto/hash_impl_sha256.cpp') diff --git a/src/buildtool/crypto/hash_impl_sha256.cpp b/src/buildtool/crypto/hash_impl_sha256.cpp new file mode 100644 index 00000000..bbea10d3 --- /dev/null +++ b/src/buildtool/crypto/hash_impl_sha256.cpp @@ -0,0 +1,50 @@ +#include +#include + +#include "openssl/sha.h" +#include "src/buildtool/crypto/hash_impl.hpp" + +/// \brief Hash implementation for SHA-256 +class HashImplSha256 final : public IHashImpl { + public: + HashImplSha256() { initialized_ = SHA256_Init(&ctx_) == 1; } + + auto Update(std::string const& data) noexcept -> bool final { + return initialized_ and + SHA256_Update(&ctx_, data.data(), data.size()) == 1; + } + + auto Finalize() && noexcept -> std::optional final { + if (initialized_) { + auto out = std::array{}; + if (SHA256_Final(out.data(), &ctx_) == 1) { + return std::string{out.begin(), out.end()}; + } + } + return std::nullopt; + } + + auto Compute(std::string const& data) && noexcept -> std::string final { + if (Update(data)) { + auto digest = std::move(*this).Finalize(); + if (digest) { + return *digest; + } + } + FatalError(); + return {}; + } + + [[nodiscard]] auto DigestLength() const noexcept -> std::size_t final { + return SHA256_DIGEST_LENGTH; + } + + private: + SHA256_CTX ctx_{}; + bool initialized_{}; +}; + +/// \brief Factory for SHA-256 implementation +auto CreateHashImplSha256() -> std::unique_ptr { + return std::make_unique(); +} -- cgit v1.2.3