Nix  2.93.0-dev
Lix: A modern, delicious implementation of the Nix package manager; unstable internal interfaces
Loading...
Searching...
No Matches
hash.hh
Go to the documentation of this file.
1#pragma once
3
8
9
10namespace nix {
11
12
13MakeError(BadHash, Error);
14
15
16enum class HashType : char { MD5 = 42, SHA1, SHA256, SHA512 };
17
18
19const int md5HashSize = 16;
20const int sha1HashSize = 20;
21const int sha256HashSize = 32;
22const int sha512HashSize = 64;
23
24extern std::set<std::string> hashTypes;
25
26extern const std::string base32Chars;
27
28enum class Base : int { Base64, Base32, Base16, SRI };
29
30
31struct Hash
32{
33 constexpr static size_t maxHashSize = 64;
34 size_t hashSize = 0;
35 uint8_t hash[maxHashSize] = {};
36
37 HashType type;
38
42 Hash(HashType type);
43
51 static Hash parseAny(std::string_view s, std::optional<HashType> type);
52
57 static Hash parseAnyPrefixed(std::string_view s);
58
63 static Hash parseNonSRIUnprefixed(std::string_view s, HashType type);
64
65 static Hash parseSRI(std::string_view original);
66
67private:
72 Hash(std::string_view s, HashType type, bool isSRI);
73
74public:
78 bool operator == (const Hash & h2) const;
79
83 bool operator != (const Hash & h2) const;
84
88 bool operator < (const Hash & h) const;
89
93 size_t base16Len() const
94 {
95 return hashSize * 2;
96 }
97
101 size_t base32Len() const
102 {
103 return (hashSize * 8 - 1) / 5 + 1;
104 }
105
109 size_t base64Len() const
110 {
111 return ((4 * hashSize / 3) + 3) & ~3;
112 }
113
119 std::string to_string(Base base, bool includeType) const;
120
121 std::string gitRev() const
122 {
123 return to_string(Base::Base16, false);
124 }
125
126 std::string gitShortRev() const
127 {
128 return std::string(to_string(Base::Base16, false), 0, 7);
129 }
130
131 static Hash dummy;
132};
133
137Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashType> ht);
138
142std::string printHash16or32(const Hash & hash);
143
147Hash hashString(HashType ht, std::string_view s);
148
152Hash hashFile(HashType ht, const Path & path);
153
158typedef std::pair<Hash, uint64_t> HashResult;
159HashResult hashPath(HashType ht, const PreparedDump & path);
160inline HashResult hashPath(HashType ht, Path path)
161{
162 return hashPath(ht, *prepareDump(std::move(path)));
163}
164
169Hash compressHash(const Hash & hash, unsigned int newSize);
170
174HashType parseHashType(std::string_view s);
175
179std::optional<HashType> parseHashTypeOpt(std::string_view s);
180
184std::string_view printHashType(HashType ht);
185
186
187union Ctx;
188
189struct AbstractHashSink : virtual Sink
190{
191 virtual HashResult finish() = 0;
192};
193
195{
196private:
197 HashType ht;
198 Ctx * ctx;
199 uint64_t bytes;
200
201public:
202 HashSink(HashType ht);
203 HashSink(const HashSink & h);
204 ~HashSink();
205 void writeUnbuffered(std::string_view data) override;
206 HashResult finish() override;
207 HashResult currentHash();
208};
209
210inline HashResult hashSource(HashType ht, Source & source)
211{
212 HashSink h(ht);
213 source.drainInto(h);
214 return h.finish();
215}
216
217
218}
Definition hash.hh:195
std::pair< Hash, uint64_t > HashResult
Definition hash.hh:158
Definition hash.hh:190
Definition serialise.hh:45
Definition hash.hh:32
static Hash parseAnyPrefixed(std::string_view s)
Definition hash.cc:176
size_t base64Len() const
Definition hash.hh:109
size_t base16Len() const
Definition hash.hh:93
bool operator==(const Hash &h2) const
Definition hash.cc:43
static Hash parseNonSRIUnprefixed(std::string_view s, HashType type)
Definition hash.cc:205
size_t base32Len() const
Definition hash.hh:101
bool operator!=(const Hash &h2) const
Definition hash.cc:52
std::string to_string(Base base, bool includeType) const
Definition hash.cc:118
bool operator<(const Hash &h) const
Definition hash.cc:58
static Hash parseAny(std::string_view s, std::optional< HashType > type)
Definition hash.cc:189
Hash(HashType type)
Definition hash.cc:35
Definition archive.hh:76
Definition serialise.hh:18
Definition serialise.hh:66
std::string Path
Definition types.hh:28
Definition hash.cc:278