summaryrefslogtreecommitdiff
path: root/src/buildtool/crypto/hash_impl_sha1.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_sha1.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_sha1.cpp')
-rw-r--r--src/buildtool/crypto/hash_impl_sha1.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/buildtool/crypto/hash_impl_sha1.cpp b/src/buildtool/crypto/hash_impl_sha1.cpp
new file mode 100644
index 00000000..e0bee0fb
--- /dev/null
+++ b/src/buildtool/crypto/hash_impl_sha1.cpp
@@ -0,0 +1,50 @@
+#include <array>
+#include <cstdint>
+
+#include "openssl/sha.h"
+#include "src/buildtool/crypto/hash_impl.hpp"
+
+/// \brief Hash implementation for SHA-1
+class HashImplSha1 final : public IHashImpl {
+ public:
+ HashImplSha1() { initialized_ = SHA1_Init(&ctx_) == 1; }
+
+ auto Update(std::string const& data) noexcept -> bool final {
+ return initialized_ and
+ SHA1_Update(&ctx_, data.data(), data.size()) == 1;
+ }
+
+ auto Finalize() && noexcept -> std::optional<std::string> final {
+ if (initialized_) {
+ auto out = std::array<std::uint8_t, SHA_DIGEST_LENGTH>{};
+ if (SHA1_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 SHA_DIGEST_LENGTH;
+ }
+
+ private:
+ SHA_CTX ctx_{};
+ bool initialized_{};
+};
+
+/// \brief Factory for SHA-1 implementation
+auto CreateHashImplSha1() -> std::unique_ptr<IHashImpl> {
+ return std::make_unique<HashImplSha1>();
+}