Nix 2.26.3
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
4#include "types.hh"
5#include "serialise.hh"
6#include "file-system.hh"
7
8namespace nix {
9
10
11MakeError(BadHash, Error);
12
13
14enum struct HashAlgorithm : char { MD5 = 42, SHA1, SHA256, SHA512 };
15
16
17const int md5HashSize = 16;
18const int sha1HashSize = 20;
19const int sha256HashSize = 32;
20const int sha512HashSize = 64;
21
22extern const std::set<std::string> hashAlgorithms;
23
24extern const std::string nix32Chars;
25
29enum struct HashFormat : int {
40};
41
42extern const std::set<std::string> hashFormats;
43
44struct Hash
45{
46 constexpr static size_t maxHashSize = 64;
47 size_t hashSize = 0;
48 uint8_t hash[maxHashSize] = {};
49
50 HashAlgorithm algo;
51
55 explicit Hash(HashAlgorithm algo);
56
64 static Hash parseAny(std::string_view s, std::optional<HashAlgorithm> optAlgo);
65
70 static Hash parseAnyPrefixed(std::string_view s);
71
76 static Hash parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo);
77
78 static Hash parseSRI(std::string_view original);
79
80private:
85 Hash(std::string_view s, HashAlgorithm algo, bool isSRI);
86
87public:
91 bool operator == (const Hash & h2) const noexcept;
92
96 std::strong_ordering operator <=> (const Hash & h2) const noexcept;
97
101 [[nodiscard]] size_t base16Len() const
102 {
103 return hashSize * 2;
104 }
105
109 [[nodiscard]] size_t base32Len() const
110 {
111 return (hashSize * 8 - 1) / 5 + 1;
112 }
113
117 [[nodiscard]] size_t base64Len() const
118 {
119 return ((4 * hashSize / 3) + 3) & ~3;
120 }
121
127 [[nodiscard]] std::string to_string(HashFormat hashFormat, bool includeAlgo) const;
128
129 [[nodiscard]] std::string gitRev() const
130 {
131 return to_string(HashFormat::Base16, false);
132 }
133
134 [[nodiscard]] std::string gitShortRev() const
135 {
136 return std::string(to_string(HashFormat::Base16, false), 0, 7);
137 }
138
139 static Hash dummy;
140
144 static Hash random(HashAlgorithm algo);
145};
146
150Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashAlgorithm> ha);
151
155std::string printHash16or32(const Hash & hash);
156
160Hash hashString(HashAlgorithm ha, std::string_view s);
161
167Hash hashFile(HashAlgorithm ha, const Path & path);
168
174typedef std::pair<Hash, uint64_t> HashResult;
175
180Hash compressHash(const Hash & hash, unsigned int newSize);
181
185HashFormat parseHashFormat(std::string_view hashFormatName);
186
190std::optional<HashFormat> parseHashFormatOpt(std::string_view hashFormatName);
191
195std::string_view printHashFormat(HashFormat hashFormat);
196
200HashAlgorithm parseHashAlgo(std::string_view s);
201
205std::optional<HashAlgorithm> parseHashAlgoOpt(std::string_view s);
206
210std::string_view printHashAlgo(HashAlgorithm ha);
211
212
213union Ctx;
214
215struct AbstractHashSink : virtual Sink
216{
217 virtual HashResult finish() = 0;
218};
219
220class HashSink : public BufferedSink, public AbstractHashSink
221{
222private:
223 HashAlgorithm ha;
224 Ctx * ctx;
225 uint64_t bytes;
226
227public:
228 HashSink(HashAlgorithm ha);
229 HashSink(const HashSink & h);
230 ~HashSink();
231 void writeUnbuffered(std::string_view data) override;
232 HashResult finish() override;
233 HashResult currentHash();
234};
235
236
237}
return s
Definition lexer.l:459
std::variant< std::string, std::string_view > data
Definition lexer.l:177
std::pair< Hash, uint64_t > HashResult
Definition hash.hh:174
HashFormat
Enumeration representing the hash formats.
Definition hash.hh:29
@ Base16
Lowercase hexadecimal encoding.
Definition hash.hh:36
@ Base64
Base 64 encoding.
Definition hash.hh:32
@ Nix32
Nix-specific base-32 encoding.
Definition hash.hh:34
@ SRI
"<hash algo>:<Base 64 hash>", format of the SRI integrity attribute.
Definition hash.hh:39
Definition hash.hh:216
Definition hash.hh:45
static Hash parseAnyPrefixed(std::string_view s)
Definition hash.cc:172
size_t base64Len() const
Definition hash.hh:117
size_t base16Len() const
Definition hash.hh:101
static Hash random(HashAlgorithm algo)
Definition hash.cc:265
static Hash parseNonSRIUnprefixed(std::string_view s, HashAlgorithm algo)
Definition hash.cc:201
std::strong_ordering operator<=>(const Hash &h2) const noexcept
Definition hash.cc:53
size_t base32Len() const
Definition hash.hh:109
bool operator==(const Hash &h2) const noexcept
Definition hash.cc:44
Hash(HashAlgorithm algo)
Definition hash.cc:36
static Hash parseAny(std::string_view s, std::optional< HashAlgorithm > optAlgo)
Definition hash.cc:185
Definition serialise.hh:20
std::string Path
Definition types.hh:22
Definition hash.cc:286