| 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 |
|
| 11 | namespace nix {
|
| 12 |
|
| 13 | |
| 14 | inline const string base32Chars {"0123456789abcdfghijklmnpqrsvwxyz"};
|
| 15 |
|
| 16 | extern const std::vector<char> base32Values;
|
| 17 |
|
| 18 | typedef 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 |
|
| 29 | struct Hash
|
| 30 | {
|
| 31 | static const unsigned int maxHashSize = 64;
|
| 32 | unsigned int hashSize;
|
| 33 | unsigned char hash[maxHashSize];
|
| 34 |
|
| 35 | HashType type;
|
| 36 |
|
| 37 |
|
| 38 | Hash();
|
| 39 |
|
| 40 |
|
| 41 | Hash(HashType type);
|
| 42 |
|
| 43 |
|
| 44 | bool operator == (const Hash & h2) const;
|
| 45 |
|
| 46 |
|
| 47 | bool operator != (const Hash & h2) const;
|
| 48 |
|
| 49 |
|
| 50 | bool operator < (const Hash & h) const;
|
| 51 | };
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 | string printHash(const Hash & hash);
|
| 56 |
|
| 57 |
|
| 58 | Hash parseHash(HashType ht, std::string_view s);
|
| 59 |
|
| 60 |
|
| 61 | unsigned int hashLength32(const Hash & hash);
|
| 62 |
|
| 63 |
|
| 64 | string printHash32(const Hash & hash);
|
| 65 |
|
| 66 |
|
| 67 | string printHash16or32(const Hash & hash);
|
| 68 |
|
| 69 |
|
| 70 | Hash parseHash32(HashType ht, std::string_view s);
|
| 71 |
|
| 72 |
|
| 73 | Hash parseHash16or32(HashType ht, std::string_view s);
|
| 74 |
|
| 75 | |
| 76 | characters.
|
| 77 | bool isHash(std::string_view s);
|
| 78 |
|
| 79 | |
| 80 | characters.
|
| 81 | bool isHash32(std::string_view s);
|
| 82 |
|
| 83 |
|
| 84 | Hash hashString(HashType ht, const string & s);
|
| 85 |
|
| 86 |
|
| 87 | Hash hashFile(HashType ht, const Path & path);
|
| 88 |
|
| 89 | |
| 90 | (essentially) hashString(ht, dumpPath(path)).
|
| 91 | typedef std::pair<Hash, unsigned long long> HashResult;
|
| 92 | HashResult hashPath(HashType ht, const Path & path,
|
| 93 | PathFilter & filter = defaultPathFilter);
|
| 94 |
|
| 95 | |
| 96 | XORing bytes together.
|
| 97 | Hash compressHash(const Hash & hash, unsigned int newSize);
|
| 98 |
|
| 99 |
|
| 100 | HashType parseHashType(const string & s);
|
| 101 |
|
| 102 |
|
| 103 | string printHashType(HashType ht);
|
| 104 |
|
| 105 |
|
| 106 | struct Ctx;
|
| 107 |
|
| 108 | class HashSink : public BufferedSink
|
| 109 | {
|
| 110 | private:
|
| 111 | HashType ht;
|
| 112 | Ctx * ctx;
|
| 113 | unsigned long long bytes;
|
| 114 |
|
| 115 | public:
|
| 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 | }
|