Nix 2.93.3
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
4#include <openssl/evp.h>
5
10
11
12namespace nix {
13
14
15namespace detail {
16
17using EvpMdCtxPtr = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>;
18
19}
20
21
22MakeError(BadHash, Error);
23
24
25enum class HashType : char { MD5 = 42, SHA1, SHA256, SHA512 };
26
27
28const int md5HashSize = 16;
29const int sha1HashSize = 20;
30const int sha256HashSize = 32;
31const int sha512HashSize = 64;
32
33extern std::set<std::string> hashTypes;
34
35extern const std::string base32Chars;
36
37enum class Base : int { Base64, Base32, Base16, SRI };
38
39
40struct Hash
41{
42 constexpr static size_t maxHashSize = 64;
43 size_t hashSize = 0;
44 uint8_t hash[maxHashSize] = {};
45
46 HashType type;
47
51 Hash(HashType type);
52
60 static Hash parseAny(std::string_view s, std::optional<HashType> type);
61
66 static Hash parseAnyPrefixed(std::string_view s);
67
72 static Hash parseNonSRIUnprefixed(std::string_view s, HashType type);
73
74 static Hash parseSRI(std::string_view original);
75
76private:
81 Hash(std::string_view s, HashType type, bool isSRI);
82
83public:
87 bool operator == (const Hash & h2) const;
88
92 bool operator != (const Hash & h2) const;
93
97 bool operator < (const Hash & h) const;
98
102 size_t base16Len() const
103 {
104 return hashSize * 2;
105 }
106
110 size_t base32Len() const
111 {
112 return (hashSize * 8 - 1) / 5 + 1;
113 }
114
118 size_t base64Len() const
119 {
120 return ((4 * hashSize / 3) + 3) & ~3;
121 }
122
128 std::string to_string(Base base, bool includeType) const;
129
130 std::string gitRev() const
131 {
132 return to_string(Base::Base16, false);
133 }
134
135 std::string gitShortRev() const
136 {
137 return std::string(to_string(Base::Base16, false), 0, 7);
138 }
139
140 static Hash dummy;
141};
142
146Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashType> ht);
147
151std::string printHash16or32(const Hash & hash);
152
156Hash hashString(HashType ht, std::string_view s);
157
161Hash hashFile(HashType ht, const Path & path);
162
167typedef std::pair<Hash, uint64_t> HashResult;
168HashResult hashPath(HashType ht, const PreparedDump & path);
169inline HashResult hashPath(HashType ht, Path path)
170{
171 return hashPath(ht, *prepareDump(std::move(path)));
172}
173
178Hash compressHash(const Hash & hash, unsigned int newSize);
179
183HashType parseHashType(std::string_view s);
184
188std::optional<HashType> parseHashTypeOpt(std::string_view s);
189
193std::string_view printHashType(HashType ht);
194
195
196struct AbstractHashSink : virtual Sink
197{
198 virtual HashResult finish() = 0;
199};
200
201class HashSink : public BufferedSink, public AbstractHashSink
202{
203private:
204 HashType ht;
205 detail::EvpMdCtxPtr ctx;
206 uint64_t bytes;
207
208public:
209 HashSink(HashType ht);
210 HashSink(const HashSink & h);
211 ~HashSink();
212 void writeUnbuffered(std::string_view data) override;
213 HashResult finish() override;
214 HashResult currentHash();
215};
216
217inline HashResult hashSource(HashType ht, Source & source)
218{
219 HashSink h(ht);
220 source.drainInto(h);
221 return h.finish();
222}
223
224
225}
Definition hash.hh:202
std::pair< Hash, uint64_t > HashResult
Definition hash.hh:167
Definition hash.hh:197
Definition hash.hh:41
static Hash parseAnyPrefixed(std::string_view s)
Definition hash.cc:174
size_t base64Len() const
Definition hash.hh:118
size_t base16Len() const
Definition hash.hh:102
bool operator==(const Hash &h2) const
Definition hash.cc:41
static Hash parseNonSRIUnprefixed(std::string_view s, HashType type)
Definition hash.cc:203
size_t base32Len() const
Definition hash.hh:110
bool operator!=(const Hash &h2) const
Definition hash.cc:50
std::string to_string(Base base, bool includeType) const
Definition hash.cc:116
bool operator<(const Hash &h) const
Definition hash.cc:56
static Hash parseAny(std::string_view s, std::optional< HashType > type)
Definition hash.cc:187
Hash(HashType type)
Definition hash.cc:33
Definition archive.hh:76
Definition serialise.hh:18
Definition serialise.hh:66
std::string Path
Definition types.hh:28