summaryrefslogtreecommitdiff
path: root/src/buildtool/crypto/hash_impl_sha256.cpp
diff options
context:
space:
mode:
authorMaksim Denisov <denisov.maksim@huawei.com>2024-07-17 16:26:25 +0200
committerMaksim Denisov <denisov.maksim@huawei.com>2024-07-22 17:02:34 +0200
commita5be6de770c9152f3248ce0f01ac3645e30a19ed (patch)
treef47ff9a1f4947b7b106acb4120a8f77ace5e2671 /src/buildtool/crypto/hash_impl_sha256.cpp
parent21014f3be3e4f8ebf34243f4f10841f0fb0b3843 (diff)
downloadjustbuild-a5be6de770c9152f3248ce0f01ac3645e30a19ed.tar.gz
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.
Diffstat (limited to 'src/buildtool/crypto/hash_impl_sha256.cpp')
-rw-r--r--src/buildtool/crypto/hash_impl_sha256.cpp54
1 files changed, 0 insertions, 54 deletions
diff --git a/src/buildtool/crypto/hash_impl_sha256.cpp b/src/buildtool/crypto/hash_impl_sha256.cpp
deleted file mode 100644
index 8aa4d04f..00000000
--- a/src/buildtool/crypto/hash_impl_sha256.cpp
+++ /dev/null
@@ -1,54 +0,0 @@
-// Copyright 2022 Huawei Cloud Computing Technology Co., Ltd.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "src/buildtool/crypto/hash_impl_sha256.hpp"
-
-#include <array>
-#include <cstdint>
-
-#include "openssl/sha.h"
-
-/// \brief Hash implementation for SHA-256
-class HashImplSha256 final : public Hasher::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<std::string> final {
- if (initialized_) {
- auto out = std::array<std::uint8_t, SHA256_DIGEST_LENGTH>{};
- if (SHA256_Final(out.data(), &ctx_) == 1) {
- return std::string{out.begin(), out.end()};
- }
- }
- return std::nullopt;
- }
-
- [[nodiscard]] auto GetHashLength() const noexcept -> size_t final {
- return SHA256_DIGEST_LENGTH * kCharsPerNumber;
- }
-
- private:
- SHA256_CTX ctx_{};
- bool initialized_{};
-};
-
-/// \brief Factory for SHA-256 implementation
-auto CreateHashImplSha256() -> std::unique_ptr<Hasher::IHashImpl> {
- return std::make_unique<HashImplSha256>();
-}