diff options
-rw-r--r-- | src/buildtool/crypto/TARGETS | 14 | ||||
-rw-r--r-- | src/buildtool/crypto/hash_info.cpp | 111 | ||||
-rw-r--r-- | src/buildtool/crypto/hash_info.hpp | 106 | ||||
-rw-r--r-- | src/utils/cpp/hex_string.hpp | 8 |
4 files changed, 239 insertions, 0 deletions
diff --git a/src/buildtool/crypto/TARGETS b/src/buildtool/crypto/TARGETS index fba8ba84..4cacb13f 100644 --- a/src/buildtool/crypto/TARGETS +++ b/src/buildtool/crypto/TARGETS @@ -24,4 +24,18 @@ ] , "stage": ["src", "buildtool", "crypto"] } +, "hash_info": + { "type": ["@", "rules", "CC", "library"] + , "name": ["hash_info"] + , "hdrs": ["hash_info.hpp"] + , "srcs": ["hash_info.cpp"] + , "deps": ["hash_function", ["src/utils/cpp", "expected"]] + , "private-deps": + [ "hasher" + , ["@", "gsl", "", "gsl"] + , ["@", "fmt", "", "fmt"] + , ["src/utils/cpp", "hex_string"] + ] + , "stage": ["src", "buildtool", "crypto"] + } } diff --git a/src/buildtool/crypto/hash_info.cpp b/src/buildtool/crypto/hash_info.cpp new file mode 100644 index 00000000..3b14d445 --- /dev/null +++ b/src/buildtool/crypto/hash_info.cpp @@ -0,0 +1,111 @@ +// Copyright 2024 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_info.hpp" + +#include "fmt/core.h" +#include "gsl/gsl" // Ensures +#include "src/buildtool/crypto/hasher.hpp" +#include "src/utils/cpp/hex_string.hpp" + +namespace { +[[nodiscard]] inline auto GetHashTypeName(HashFunction::Type type) noexcept + -> std::string { + switch (type) { + case HashFunction::Type::GitSHA1: + return "GitSHA1"; + case HashFunction::Type::PlainSHA256: + return "PlainSHA256"; + } + Ensures(false); +} + +inline constexpr auto kSHA1EmptyGitBlobHash = + "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"; +} // namespace + +HashInfo::HashInfo() noexcept + : hash_{kSHA1EmptyGitBlobHash}, + hash_type_{HashFunction::Type::GitSHA1}, + is_tree_{false} {} + +auto HashInfo::Create(HashFunction::Type type, + std::string hash, + bool is_tree) noexcept + -> expected<HashInfo, std::string> { + if (auto error = HashInfo::ValidateInput(type, hash, is_tree)) { + return unexpected{*std::move(error)}; + } + return HashInfo(std::move(hash), type, is_tree); +} + +auto HashInfo::HashData(HashFunction hash_function, + std::string const& content, + bool is_tree) noexcept -> HashInfo { + auto const hash_digest = is_tree ? hash_function.HashTreeData(content) + : hash_function.HashBlobData(content); + return HashInfo{ + hash_digest.HexString(), + hash_function.GetType(), + is_tree and hash_function.GetType() == HashFunction::Type::GitSHA1}; +} + +auto HashInfo::HashFile(HashFunction hash_function, + std::filesystem::path const& path, + bool is_tree) noexcept + -> std::optional<std::pair<HashInfo, std::uintmax_t>> { + auto const hash_digest = is_tree ? hash_function.HashTreeFile(path) + : hash_function.HashBlobFile(path); + if (not hash_digest) { + return std::nullopt; + } + return std::pair{HashInfo{hash_digest->first.HexString(), + hash_function.GetType(), + is_tree and hash_function.GetType() == + HashFunction::Type::GitSHA1}, + hash_digest->second}; +} + +auto HashInfo::operator==(HashInfo const& other) const noexcept -> bool { + return hash_ == other.hash_ and is_tree_ == other.is_tree_; +} + +auto HashInfo::ValidateInput(HashFunction::Type type, + std::string const& hash, + bool is_tree) noexcept + -> std::optional<std::string> { + if (type != HashFunction::Type::GitSHA1 and is_tree) { + return fmt::format( + "HashInfo: hash {} is expected to be {}.\nTrees are " + "not allowed in this mode.", + hash, + GetHashTypeName(type)); + } + + if (auto const exp_size = HashFunction{type}.MakeHasher().GetHashLength(); + hash.size() != exp_size) { + return fmt::format( + "HashInfo: hash {} is expected to be {}.\n It must have a length " + "of {}, but its length is {}.", + hash, + GetHashTypeName(type), + exp_size, + hash.size()); + } + + if (not IsHexString(hash)) { + return fmt::format("HashInfo: Invalid hash {}", hash); + } + return std::nullopt; +} diff --git a/src/buildtool/crypto/hash_info.hpp b/src/buildtool/crypto/hash_info.hpp new file mode 100644 index 00000000..dcfde884 --- /dev/null +++ b/src/buildtool/crypto/hash_info.hpp @@ -0,0 +1,106 @@ +// Copyright 2024 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_INFO_HPP +#define INCLUDED_SRC_BUILDTOOL_CRYPTO_HASH_INFO_HPP + +#include <filesystem> +#include <optional> +#include <string> +#include <utility> + +#include "src/buildtool/crypto/hash_function.hpp" +#include "src/utils/cpp/expected.hpp" + +/// \brief A collection of data related to a specific hash. Once it is +/// constructed, it holds a valid hexadecimal (always unprefixed) hash with some +/// additional information about the method of hashing. +class HashInfo final { + public: + explicit HashInfo() noexcept; + + /// \brief Build HashInfo based on 'external' data that cannot be trusted. A + /// number of validation checks is happening + /// \param type Type of the hash function used to create the hash + /// \param hash A hexadecimal hash + /// \param is_tree Tree or blob. Note that trees are not allowed in the + /// compatible mode. + /// \return Validated HashInfo on success or an error message on failure. + [[nodiscard]] static auto Create(HashFunction::Type type, + std::string hash, + bool is_tree) noexcept + -> expected<HashInfo, std::string>; + + /// \brief Hash content and build HashInfo + /// \param hash_function Hash function to be used + /// \param content Content to be hashed + /// \param is_tree Tree or blob, the type of the algorithm to be used for + /// hashing. Note that HashInfo may return another value from IsTree in + /// compatible mode. + [[nodiscard]] static auto HashData(HashFunction hash_function, + std::string const& content, + bool is_tree) noexcept -> HashInfo; + + /// \brief Hash file and build HashInfo + /// \param hash_function Hash function to be use + /// \param path File to be hashed + /// \param is_tree Tree or blob, the type of the algorithm to be used for + /// hashing. Note that HashInfo may return another value from IsTree in + /// compatible mode. + /// \return A combination of the hash of the file and file's size or + /// std::nullopt on IO failure. + [[nodiscard]] static auto HashFile(HashFunction hash_function, + std::filesystem::path const& path, + bool is_tree) noexcept + -> std::optional<std::pair<HashInfo, std::uintmax_t>>; + + [[nodiscard]] inline auto Hash() const& noexcept -> std::string const& { + return hash_; + } + + [[nodiscard]] inline auto Hash() && -> std::string { + return std::move(hash_); + } + + [[nodiscard]] inline auto HashType() const noexcept -> HashFunction::Type { + return hash_type_; + } + + [[nodiscard]] inline auto IsTree() const noexcept -> bool { + return is_tree_; + } + + [[nodiscard]] auto operator==(HashInfo const& other) const noexcept -> bool; + + private: + std::string hash_; + HashFunction::Type hash_type_; + + /// \brief Tree or blob algorithm was used for hashing. is_tree_ can be true + /// in the native mode only, in compatible it falls back to false during + /// hashing via HashData/HashFile or an error occurs during validation. + bool is_tree_; + + explicit HashInfo(std::string hash, + HashFunction::Type type, + bool is_tree) noexcept + : hash_{std::move(hash)}, hash_type_{type}, is_tree_{is_tree} {} + + [[nodiscard]] static auto ValidateInput(HashFunction::Type type, + std::string const& hash, + bool is_tree) noexcept + -> std::optional<std::string>; +}; + +#endif // INCLUDED_SRC_BUILDTOOL_CRYPTO_HASH_INFO_HPP diff --git a/src/utils/cpp/hex_string.hpp b/src/utils/cpp/hex_string.hpp index 23544d49..4b0124c4 100644 --- a/src/utils/cpp/hex_string.hpp +++ b/src/utils/cpp/hex_string.hpp @@ -15,6 +15,8 @@ #ifndef INCLUDED_SRC_UTILS_CPP_HEX_STRING_HPP #define INCLUDED_SRC_UTILS_CPP_HEX_STRING_HPP +#include <algorithm> +#include <cctype> #include <cstddef> #include <iomanip> #include <iostream> @@ -22,6 +24,12 @@ #include <sstream> #include <string> +[[nodiscard]] static inline auto IsHexString(std::string const& s) noexcept + -> bool { + return std::all_of( + s.begin(), s.end(), [](unsigned char c) { return std::isxdigit(c); }); +} + [[nodiscard]] static inline auto ToHexString(std::string const& bytes) -> std::string { std::ostringstream ss{}; |