diff options
author | Maksim Denisov <denisov.maksim@huawei.com> | 2024-07-17 16:26:25 +0200 |
---|---|---|
committer | Maksim Denisov <denisov.maksim@huawei.com> | 2024-07-22 17:02:34 +0200 |
commit | a5be6de770c9152f3248ce0f01ac3645e30a19ed (patch) | |
tree | f47ff9a1f4947b7b106acb4120a8f77ace5e2671 /src/buildtool/crypto/hash_impl_sha1.cpp | |
parent | 21014f3be3e4f8ebf34243f4f10841f0fb0b3843 (diff) | |
download | justbuild-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_sha1.cpp')
-rw-r--r-- | src/buildtool/crypto/hash_impl_sha1.cpp | 54 |
1 files changed, 0 insertions, 54 deletions
diff --git a/src/buildtool/crypto/hash_impl_sha1.cpp b/src/buildtool/crypto/hash_impl_sha1.cpp deleted file mode 100644 index c72046d1..00000000 --- a/src/buildtool/crypto/hash_impl_sha1.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_sha1.hpp" - -#include <array> -#include <cstdint> - -#include "openssl/sha.h" - -/// \brief Hash implementation for SHA-1 -class HashImplSha1 final : public Hasher::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; - } - - [[nodiscard]] auto GetHashLength() const noexcept -> size_t final { - return SHA_DIGEST_LENGTH * kCharsPerNumber; - } - - private: - SHA_CTX ctx_{}; - bool initialized_{}; -}; - -/// \brief Factory for SHA-1 implementation -auto CreateHashImplSha1() -> std::unique_ptr<Hasher::IHashImpl> { - return std::make_unique<HashImplSha1>(); -} |