summaryrefslogtreecommitdiff
path: root/src/buildtool/crypto/hash_function.hpp
blob: ac7cadcc38f29476d623f6d29e98811691ed9647 (plain)
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
// 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 <functional>
#include <optional>
#include <string>

#include <gsl-lite/gsl-lite.hpp>

#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.
    };

    /// \brief Set globally used hash type.
    static void SetHashType(JustHash type) {
        [[maybe_unused]] auto _ = HashType(type);
    }

    /// \brief Compute a plain hash.
    [[nodiscard]] static auto ComputeHash(std::string const& data) noexcept
        -> Hasher::HashDigest {
        return ComputeTaggedHash(data);
    }

    /// \brief Compute a blob hash.
    [[nodiscard]] static auto ComputeBlobHash(std::string const& data) 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 a tree hash.
    [[nodiscard]] static auto ComputeTreeHash(std::string const& data) 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]] static auto Hasher() noexcept -> ::Hasher {
        switch (HashType()) {
            case JustHash::Native:
                return ::Hasher{Hasher::HashType::SHA1};
            case JustHash::Compatible:
                return ::Hasher{Hasher::HashType::SHA256};
        }
        gsl_Ensures(false);  // unreachable
    }

  private:
    static constexpr auto kDefaultType = JustHash::Native;

    [[nodiscard]] static auto HashType(
        std::optional<JustHash> type = std::nullopt) -> JustHash {
        static JustHash type_{kDefaultType};
        if (type) {
            type_ = *type;
        }
        return type_;
    }

    [[nodiscard]] static auto ComputeTaggedHash(
        std::string const& data,
        std::function<std::string(std::string const&)> const& tag_creator =
            {}) noexcept -> Hasher::HashDigest {
        auto hasher = Hasher();
        if (tag_creator and HashType() == JustHash::Native) {
            hasher.Update(tag_creator(data));
        }
        hasher.Update(data);
        return std::move(hasher).Finalize();
    }
};

#endif  // INCLUDED_SRC_BUILDTOOL_CRYPTO_HASH_FUNCTION_HPP