summaryrefslogtreecommitdiff
path: root/src/buildtool/crypto/hash_impl_git.cpp
diff options
context:
space:
mode:
authorKlaus Aehlig <klaus.aehlig@huawei.com>2022-02-22 17:03:21 +0100
committerKlaus Aehlig <klaus.aehlig@huawei.com>2022-02-22 17:03:21 +0100
commit619def44c1cca9f3cdf63544d5f24f2c7a7d9b77 (patch)
tree01868de723cb82c86842f33743fa7b14e24c1fa3 /src/buildtool/crypto/hash_impl_git.cpp
downloadjustbuild-619def44c1cca9f3cdf63544d5f24f2c7a7d9b77.tar.gz
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 <oliver.reiche@huawei.com> Co-authored-by: Victor Moreno <victor.moreno1@huawei.com>
Diffstat (limited to 'src/buildtool/crypto/hash_impl_git.cpp')
-rw-r--r--src/buildtool/crypto/hash_impl_git.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/buildtool/crypto/hash_impl_git.cpp b/src/buildtool/crypto/hash_impl_git.cpp
new file mode 100644
index 00000000..9cb2a761
--- /dev/null
+++ b/src/buildtool/crypto/hash_impl_git.cpp
@@ -0,0 +1,42 @@
+#include <array>
+#include <cstdint>
+
+#include "openssl/sha.h"
+#include "src/buildtool/crypto/hash_impl.hpp"
+
+/// \brief Hash implementation for Git blob ids.
+/// Does not support incremental hashing.
+class HashImplGit final : public IHashImpl {
+ public:
+ auto Update(std::string const& /*data*/) noexcept -> bool final {
+ return false;
+ }
+
+ auto Finalize() && noexcept -> std::optional<std::string> final {
+ return std::nullopt;
+ }
+
+ auto Compute(std::string const& data) && noexcept -> std::string final {
+ SHA_CTX ctx;
+ std::string const header{"blob " + std::to_string(data.size()) + '\0'};
+ if (SHA1_Init(&ctx) == 1 &&
+ SHA1_Update(&ctx, header.data(), header.size()) == 1 &&
+ SHA1_Update(&ctx, data.data(), data.size()) == 1) {
+ auto out = std::array<std::uint8_t, SHA_DIGEST_LENGTH>{};
+ if (SHA1_Final(out.data(), &ctx) == 1) {
+ return std::string{out.begin(), out.end()};
+ }
+ }
+ FatalError();
+ return {};
+ }
+
+ [[nodiscard]] auto DigestLength() const noexcept -> std::size_t final {
+ return SHA_DIGEST_LENGTH;
+ }
+};
+
+/// \brief Factory for Git implementation
+auto CreateHashImplGit() -> std::unique_ptr<IHashImpl> {
+ return std::make_unique<HashImplGit>();
+}