diff options
author | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2022-09-06 10:49:19 +0200 |
---|---|---|
committer | Paul Cristian Sarbu <paul.cristian.sarbu@huawei.com> | 2022-12-21 14:59:04 +0100 |
commit | 6ce8a51993e0dfd8feebb51ede67c479c8a78e96 (patch) | |
tree | 48ab9b7475c419be7347a1de5f6f4dc7c27e1194 | |
parent | 22e6727e44d709f8591c425699abea19d1dd2394 (diff) | |
download | justbuild-6ce8a51993e0dfd8feebb51ede67c479c8a78e96.tar.gz |
Crypto: Add SHA512 hasher
Needed by the content-in-CAS git map to check fetched archives' checksums.
SHA1 and SHA256 hashers are already implemented.
-rw-r--r-- | src/buildtool/crypto/TARGETS | 14 | ||||
-rw-r--r-- | src/buildtool/crypto/hash_impl_sha512.cpp | 61 | ||||
-rw-r--r-- | src/buildtool/crypto/hash_impl_sha512.hpp | 25 | ||||
-rw-r--r-- | src/buildtool/crypto/hasher.cpp | 3 | ||||
-rw-r--r-- | src/buildtool/crypto/hasher.hpp | 2 | ||||
-rw-r--r-- | test/buildtool/crypto/hasher.test.cpp | 8 |
6 files changed, 110 insertions, 3 deletions
diff --git a/src/buildtool/crypto/TARGETS b/src/buildtool/crypto/TARGETS index 4f73badf..9598206c 100644 --- a/src/buildtool/crypto/TARGETS +++ b/src/buildtool/crypto/TARGETS @@ -1,8 +1,18 @@ { "hasher": { "type": ["@", "rules", "CC", "library"] , "name": ["hasher"] - , "hdrs": ["hasher.hpp", "hash_impl_sha1.hpp", "hash_impl_sha256.hpp"] - , "srcs": ["hasher.cpp", "hash_impl_sha1.cpp", "hash_impl_sha256.cpp"] + , "hdrs": + [ "hasher.hpp" + , "hash_impl_sha1.hpp" + , "hash_impl_sha256.hpp" + , "hash_impl_sha512.hpp" + ] + , "srcs": + [ "hasher.cpp" + , "hash_impl_sha1.cpp" + , "hash_impl_sha256.cpp" + , "hash_impl_sha512.cpp" + ] , "stage": ["src", "buildtool", "crypto"] , "deps": [["src/buildtool/logging", "logging"], ["src/utils/cpp", "hex_string"]] diff --git a/src/buildtool/crypto/hash_impl_sha512.cpp b/src/buildtool/crypto/hash_impl_sha512.cpp new file mode 100644 index 00000000..802ebf0e --- /dev/null +++ b/src/buildtool/crypto/hash_impl_sha512.cpp @@ -0,0 +1,61 @@ +// 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_sha512.hpp" + +#include <array> +#include <cstdint> + +#include "openssl/sha.h" + +/// \brief Hash implementation for SHA-512 +class HashImplSha512 final : public Hasher::IHashImpl { + public: + HashImplSha512() { initialized_ = SHA512_Init(&ctx_) == 1; } + + auto Update(std::string const& data) noexcept -> bool final { + return initialized_ and + SHA512_Update(&ctx_, data.data(), data.size()) == 1; + } + + auto Finalize() && noexcept -> std::optional<std::string> final { + if (initialized_) { + auto out = std::array<std::uint8_t, SHA512_DIGEST_LENGTH>{}; + if (SHA512_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 {}; + } + + private: + SHA512_CTX ctx_{}; + bool initialized_{}; +}; + +/// \brief Factory for SHA-512 implementation +auto CreateHashImplSha512() -> std::unique_ptr<Hasher::IHashImpl> { + return std::make_unique<HashImplSha512>(); +} diff --git a/src/buildtool/crypto/hash_impl_sha512.hpp b/src/buildtool/crypto/hash_impl_sha512.hpp new file mode 100644 index 00000000..8a32cc42 --- /dev/null +++ b/src/buildtool/crypto/hash_impl_sha512.hpp @@ -0,0 +1,25 @@ +// 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. + +#ifndef INCLUDED_SRC_BUILDTOOL_CRYPTO_HASH_IMPL_SHA512_HPP +#define INCLUDED_SRC_BUILDTOOL_CRYPTO_HASH_IMPL_SHA512_HPP + +#include <memory> + +#include "src/buildtool/crypto/hasher.hpp" + +[[nodiscard]] extern auto CreateHashImplSha512() + -> std::unique_ptr<Hasher::IHashImpl>; + +#endif // INCLUDED_SRC_BUILDTOOL_CRYPTO_HASH_IMPL_SHA512_HPP diff --git a/src/buildtool/crypto/hasher.cpp b/src/buildtool/crypto/hasher.cpp index c3c0a3dd..60a029c2 100644 --- a/src/buildtool/crypto/hasher.cpp +++ b/src/buildtool/crypto/hasher.cpp @@ -16,6 +16,7 @@ #include "src/buildtool/crypto/hash_impl_sha1.hpp" #include "src/buildtool/crypto/hash_impl_sha256.hpp" +#include "src/buildtool/crypto/hash_impl_sha512.hpp" auto Hasher::CreateHashImpl(HashType type) noexcept -> std::unique_ptr<IHashImpl> { @@ -24,6 +25,8 @@ auto Hasher::CreateHashImpl(HashType type) noexcept return CreateHashImplSha1(); case HashType::SHA256: return CreateHashImplSha256(); + case HashType::SHA512: + return CreateHashImplSha512(); } return nullptr; // make gcc happy } diff --git a/src/buildtool/crypto/hasher.hpp b/src/buildtool/crypto/hasher.hpp index 68247038..6abf2b0c 100644 --- a/src/buildtool/crypto/hasher.hpp +++ b/src/buildtool/crypto/hasher.hpp @@ -27,7 +27,7 @@ class Hasher { public: /// \brief Types of hash implementations supported by generator. - enum class HashType : std::uint8_t { SHA1, SHA256 }; + enum class HashType : std::uint8_t { SHA1, SHA256, SHA512 }; /// \brief The universal hash digest. /// The type of hash and the digest length depends on the hash diff --git a/test/buildtool/crypto/hasher.test.cpp b/test/buildtool/crypto/hasher.test.cpp index 594e4504..45f24db7 100644 --- a/test/buildtool/crypto/hasher.test.cpp +++ b/test/buildtool/crypto/hasher.test.cpp @@ -39,4 +39,12 @@ TEST_CASE("Hasher", "[crypto]") { bytes, "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"); } + + SECTION("SHA-512") { + // same as: echo -n test | sha512sum + test_increment_hash<Hasher::HashType::SHA512>( + bytes, + "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27a" + "c185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"); + } } |