1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// 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_FUNCTION_HPP
#define INCLUDED_SRC_BUILDTOOL_CRYPTO_HASH_FUNCTION_HPP
#include <cstdint>
#include <filesystem>
#include <functional>
#include <optional>
#include <string>
#include <utility>
#include "gsl/gsl"
#include "src/buildtool/crypto/hasher.hpp"
/// \brief Hash function used for the entire buildtool.
class HashFunction {
public:
enum class JustHash : std::uint8_t {
Native, ///< SHA1 for plain hashes, and Git for blobs and trees.
Compatible ///< SHA256 for all hashes.
};
static constexpr auto kDefaultType = JustHash::Native;
explicit HashFunction(JustHash type) noexcept : type_{type} {}
[[nodiscard]] auto GetHashType() const noexcept -> JustHash {
return type_;
}
[[nodiscard]] static auto Instance() noexcept -> HashFunction& {
static HashFunction instance{kDefaultType};
return instance;
}
/// \brief Set globally used hash type.
void SetHashType(JustHash type) noexcept { type_ = type; }
/// \brief Compute a plain hash.
[[nodiscard]] auto ComputeHash(std::string const& data) const noexcept
-> Hasher::HashDigest {
return ComputeTaggedHash(data);
}
/// \brief Compute a blob hash.
[[nodiscard]] auto ComputeBlobHash(std::string const& data) const noexcept
-> Hasher::HashDigest {
static auto const kBlobTagCreator =
[](std::string const& data) -> std::string {
return {"blob " + std::to_string(data.size()) + '\0'};
};
return ComputeTaggedHash(data, kBlobTagCreator);
}
/// \brief Compute the blob hash of a file or std::nullopt on IO error.
[[nodiscard]] auto ComputeHashFile(const std::filesystem::path& file_path,
bool as_tree) const noexcept
-> std::optional<std::pair<Hasher::HashDigest, std::uintmax_t>>;
/// \brief Compute a tree hash.
[[nodiscard]] auto ComputeTreeHash(std::string const& data) const noexcept
-> Hasher::HashDigest {
static auto const kTreeTagCreator =
[](std::string const& data) -> std::string {
return {"tree " + std::to_string(data.size()) + '\0'};
};
return ComputeTaggedHash(data, kTreeTagCreator);
}
/// \brief Obtain incremental hasher for computing plain hashes.
[[nodiscard]] auto Hasher() const noexcept -> ::Hasher {
switch (type_) {
case JustHash::Native:
return ::Hasher{Hasher::HashType::SHA1};
case JustHash::Compatible:
return ::Hasher{Hasher::HashType::SHA256};
}
Ensures(false); // unreachable
}
private:
JustHash type_ = kDefaultType;
[[nodiscard]] auto ComputeTaggedHash(
std::string const& data,
std::function<std::string(std::string const&)> const& tag_creator = {})
const noexcept -> Hasher::HashDigest {
auto hasher = Hasher();
if (tag_creator and type_ == JustHash::Native) {
hasher.Update(tag_creator(data));
}
hasher.Update(data);
return std::move(hasher).Finalize();
}
};
#endif // INCLUDED_SRC_BUILDTOOL_CRYPTO_HASH_FUNCTION_HPP
|