Commit | Line | Data |
---|---|---|
36457566 LC |
1 | #pragma once |
2 | ||
3 | #include "types.hh" | |
4 | #include "serialise.hh" | |
5 | ||
6 | ||
7 | namespace nix { | |
8 | ||
9 | ||
10 | typedef enum { htUnknown, htMD5, htSHA1, htSHA256 } HashType; | |
11 | ||
12 | ||
13 | const int md5HashSize = 16; | |
14 | const int sha1HashSize = 20; | |
15 | const int sha256HashSize = 32; | |
16 | ||
17 | extern const string base32Chars; | |
18 | ||
19 | ||
20 | struct 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. */ | |
46 | string printHash(const Hash & hash); | |
47 | ||
48 | /* Parse a hexadecimal representation of a hash code. */ | |
49 | Hash parseHash(HashType ht, const string & s); | |
50 | ||
51 | /* Returns the length of a base-32 hash representation. */ | |
52 | unsigned int hashLength32(const Hash & hash); | |
53 | ||
54 | /* Convert a hash to a base-32 representation. */ | |
55 | string printHash32(const Hash & hash); | |
56 | ||
57 | /* Print a hash in base-16 if it's MD5, or base-32 otherwise. */ | |
58 | string printHash16or32(const Hash & hash); | |
59 | ||
60 | /* Parse a base-32 representation of a hash code. */ | |
61 | Hash parseHash32(HashType ht, const string & s); | |
62 | ||
63 | /* Parse a base-16 or base-32 representation of a hash code. */ | |
64 | Hash parseHash16or32(HashType ht, const string & s); | |
65 | ||
66 | /* Verify that the given string is a valid hash code. */ | |
67 | bool isHash(const string & s); | |
68 | ||
69 | /* Compute the hash of the given string. */ | |
70 | Hash hashString(HashType ht, const string & s); | |
71 | ||
72 | /* Compute the hash of the given file. */ | |
73 | Hash 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)). */ | |
77 | struct PathFilter; | |
78 | extern PathFilter defaultPathFilter; | |
79 | typedef std::pair<Hash, unsigned long long> HashResult; | |
80 | HashResult 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. */ | |
85 | Hash compressHash(const Hash & hash, unsigned int newSize); | |
86 | ||
87 | /* Parse a string representing a hash type. */ | |
88 | HashType parseHashType(const string & s); | |
89 | ||
90 | /* And the reverse. */ | |
91 | string printHashType(HashType ht); | |
92 | ||
93 | ||
94 | struct Ctx; | |
95 | ||
96 | class HashSink : public BufferedSink | |
97 | { | |
98 | private: | |
99 | HashType ht; | |
100 | Ctx * ctx; | |
101 | unsigned long long bytes; | |
102 | ||
103 | public: | |
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 | } |