projects/guix·blob python-team · refs

125 lines · 3.1 KiB

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