Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
hash.hh
Go to the documentation of this file.
1#pragma once
3
5#include "nix/util/types.hh"
9
10namespace nix {
11
12MakeError(BadHash, Error);
13
14enum struct HashAlgorithm : char { MD5 = 42, SHA1, SHA256, SHA512, BLAKE3 };
15
19constexpr inline size_t regularHashSize(HashAlgorithm type)
20{
21 switch (type) {
22 case HashAlgorithm::BLAKE3:
23 return 32;
24 case HashAlgorithm::MD5:
25 return 16;
26 case HashAlgorithm::SHA1:
27 return 20;
28 case HashAlgorithm::SHA256:
29 return 32;
30 case HashAlgorithm::SHA512:
31 return 64;
32 default:
33 assert(false);
34 }
35}
36
37extern const StringSet hashAlgorithms;
38
42enum struct HashFormat : int {
53};
54
55extern const StringSet hashFormats;
56
57struct Hash
58{
60 union Ctx;
61
62 constexpr static size_t maxHashSize = 64;
63 size_t hashSize = 0;
64 uint8_t hash[maxHashSize] = {};
65
66 HashAlgorithm algo;
67
71 explicit Hash(HashAlgorithm algo, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
72
80 static Hash parseAny(std::string_view s, std::optional<HashAlgorithm> optAlgo);
81
85 static std::pair<Hash, HashFormat>
86 parseAnyReturningFormat(std::string_view s, std::optional<HashAlgorithm> optAlgo);
87
92 static Hash parseAnyPrefixed(std::string_view s);
93
98 static Hash parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo);
99
108 std::string_view s,
109 HashAlgorithm algo,
110 HashFormat explicitFormat,
111 const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
112
113 static Hash
114 parseSRI(std::string_view original, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
115
116public:
120 bool operator==(const Hash & h2) const noexcept;
121
125 std::strong_ordering operator<=>(const Hash & h2) const noexcept;
126
132 [[nodiscard]] std::string to_string(HashFormat hashFormat, bool includeAlgo) const;
133
134 [[nodiscard]] std::string gitRev() const
135 {
136 return to_string(HashFormat::Base16, false);
137 }
138
139 [[nodiscard]] std::string gitShortRev() const
140 {
141 return std::string(to_string(HashFormat::Base16, false), 0, 7);
142 }
143
144 static Hash dummy;
145
149 static Hash random(HashAlgorithm algo);
150};
151
155Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashAlgorithm> ha);
156
160Hash hashString(
161 HashAlgorithm ha, std::string_view s, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
162
168Hash hashFile(HashAlgorithm ha, const std::filesystem::path & path);
169
174{
175 Hash hash;
176 uint64_t numBytesDigested;
177};
178
183Hash compressHash(const Hash & hash, unsigned int newSize);
184
188HashFormat parseHashFormat(std::string_view hashFormatName);
189
193std::optional<HashFormat> parseHashFormatOpt(std::string_view hashFormatName);
194
198std::string_view printHashFormat(HashFormat hashFormat);
199
203HashAlgorithm
204parseHashAlgo(std::string_view s, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
205
209std::optional<HashAlgorithm>
210parseHashAlgoOpt(std::string_view s, const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
211
215std::string_view printHashAlgo(HashAlgorithm ha);
216
217struct AbstractHashSink : virtual Sink
218{
219private:
220 void anchor() override;
221public:
222 virtual HashResult finish() = 0;
223};
224
225class HashSink : public BufferedSink, public AbstractHashSink
226{
227private:
228 HashAlgorithm ha;
229 Hash::Ctx * ctx;
230 uint64_t bytes;
231
232 void anchor() override;
233
234public:
235 HashSink(HashAlgorithm ha);
236 HashSink(const HashSink & h);
237 ~HashSink();
238 void writeUnbuffered(std::string_view data) override;
239 HashResult finish() override;
240 HashResult currentHash();
241};
242
243template<>
244struct json_avoids_null<Hash> : std::true_type
245{};
246
247} // namespace nix
248
249template<>
250struct std::hash<nix::Hash>
251{
252 std::size_t operator()(const nix::Hash & hash) const noexcept
253 {
254 assert(hash.hashSize > sizeof(size_t));
255 return *reinterpret_cast<const std::size_t *>(&hash.hash);
256 }
257};
258
259namespace nix {
260
261inline std::size_t hash_value(const Hash & hash)
262{
263 return std::hash<Hash>{}(hash);
264}
265
266} // namespace nix
267
268JSON_IMPL_WITH_XP_FEATURES(nix::Hash)
Definition error.hh:310
Utilities for working with the file system and file paths.
constexpr size_t regularHashSize(HashAlgorithm type)
Definition hash.hh:19
HashFormat
Enumeration representing the hash formats.
Definition hash.hh:42
@ Base16
Lowercase hexadecimal encoding.
Definition hash.hh:49
@ Base64
Base 64 encoding.
Definition hash.hh:45
@ Nix32
Nix-specific base-32 encoding.
Definition hash.hh:47
@ SRI
"<hash algo>:<Base 64 hash>", format of the SRI integrity attribute.
Definition hash.hh:52
Definition hash.hh:218
Definition configuration.hh:558
Definition hash.hh:174
Definition hash.hh:58
static Hash parseAnyPrefixed(std::string_view s)
Definition hash.cc:230
Hash(HashAlgorithm algo, const ExperimentalFeatureSettings &xpSettings=experimentalFeatureSettings)
Definition hash.cc:35
static Hash random(HashAlgorithm algo)
Definition hash.cc:276
static Hash parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo)
Definition hash.cc:265
static Hash parseExplicitFormatUnprefixed(std::string_view s, HashAlgorithm algo, HashFormat explicitFormat, const ExperimentalFeatureSettings &xpSettings=experimentalFeatureSettings)
Definition hash.cc:270
static std::pair< Hash, HashFormat > parseAnyReturningFormat(std::string_view s, std::optional< HashAlgorithm > optAlgo)
Definition hash.cc:251
std::strong_ordering operator<=>(const Hash &h2) const noexcept
Definition hash.cc:56
std::string to_string(HashFormat hashFormat, bool includeAlgo) const
Definition hash.cc:69
bool operator==(const Hash &h2) const noexcept
Definition hash.cc:46
static Hash parseAny(std::string_view s, std::optional< HashAlgorithm > optAlgo)
Definition hash.cc:245
Definition serialise.hh:18
Definition hash.cc:296