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