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