daemon: Fix possible use-after-free.
[jackhill/guix/guix.git] / nix / libutil / hash.hh
CommitLineData
36457566
LC
1#pragma once
2
3#include "types.hh"
4#include "serialise.hh"
5
6
7namespace nix {
8
9
10typedef enum { htUnknown, htMD5, htSHA1, htSHA256 } HashType;
11
12
13const int md5HashSize = 16;
14const int sha1HashSize = 20;
15const int sha256HashSize = 32;
16
17extern const string base32Chars;
18
19
20struct Hash
21{
22 static const unsigned int maxHashSize = 32;
23 unsigned int hashSize;
24 unsigned char hash[maxHashSize];
25
26 HashType type;
27
28 /* Create an unusable hash object. */
29 Hash();
30
31 /* Create a zero-filled hash object. */
32 Hash(HashType type);
33
34 /* Check whether two hash are equal. */
35 bool operator == (const Hash & h2) const;
36
37 /* Check whether two hash are not equal. */
38 bool operator != (const Hash & h2) const;
39
40 /* For sorting. */
41 bool operator < (const Hash & h) const;
42};
43
44
45/* Convert a hash to a hexadecimal representation. */
46string printHash(const Hash & hash);
47
48/* Parse a hexadecimal representation of a hash code. */
49Hash parseHash(HashType ht, const string & s);
50
51/* Returns the length of a base-32 hash representation. */
52unsigned int hashLength32(const Hash & hash);
53
54/* Convert a hash to a base-32 representation. */
55string printHash32(const Hash & hash);
56
57/* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
58string printHash16or32(const Hash & hash);
59
60/* Parse a base-32 representation of a hash code. */
61Hash parseHash32(HashType ht, const string & s);
62
63/* Parse a base-16 or base-32 representation of a hash code. */
64Hash parseHash16or32(HashType ht, const string & s);
65
66/* Verify that the given string is a valid hash code. */
67bool isHash(const string & s);
68
69/* Compute the hash of the given string. */
70Hash hashString(HashType ht, const string & s);
71
72/* Compute the hash of the given file. */
73Hash hashFile(HashType ht, const Path & path);
74
75/* Compute the hash of the given path. The hash is defined as
76 (essentially) hashString(ht, dumpPath(path)). */
77struct PathFilter;
78extern PathFilter defaultPathFilter;
79typedef std::pair<Hash, unsigned long long> HashResult;
80HashResult hashPath(HashType ht, const Path & path,
81 PathFilter & filter = defaultPathFilter);
82
83/* Compress a hash to the specified number of bytes by cyclically
84 XORing bytes together. */
85Hash compressHash(const Hash & hash, unsigned int newSize);
86
87/* Parse a string representing a hash type. */
88HashType parseHashType(const string & s);
89
90/* And the reverse. */
91string printHashType(HashType ht);
92
93
94struct Ctx;
95
96class HashSink : public BufferedSink
97{
98private:
99 HashType ht;
100 Ctx * ctx;
101 unsigned long long bytes;
102
103public:
104 HashSink(HashType ht);
105 HashSink(const HashSink & h);
106 ~HashSink();
107 void write(const unsigned char * data, size_t len);
108 HashResult finish();
109 HashResult currentHash();
110};
111
112
113}