6 #include "serialise.hh"
12 extern const string base32Chars
;
17 htSHA1
= GCRY_MD_SHA1
,
18 htSHA256
= GCRY_MD_SHA256
,
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
27 static const unsigned int maxHashSize
= 64;
28 unsigned int hashSize
;
29 unsigned char hash
[maxHashSize
];
33 /* Create an unusable hash object. */
36 /* Create a zero-filled hash object. */
39 /* Check whether two hash are equal. */
40 bool operator == (const Hash
& h2
) const;
42 /* Check whether two hash are not equal. */
43 bool operator != (const Hash
& h2
) const;
46 bool operator < (const Hash
& h
) const;
50 /* Convert a hash to a hexadecimal representation. */
51 string
printHash(const Hash
& hash
);
53 /* Parse a hexadecimal representation of a hash code. */
54 Hash
parseHash(HashType ht
, const string
& s
);
56 /* Returns the length of a base-32 hash representation. */
57 unsigned int hashLength32(const Hash
& hash
);
59 /* Convert a hash to a base-32 representation. */
60 string
printHash32(const Hash
& hash
);
62 /* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
63 string
printHash16or32(const Hash
& hash
);
65 /* Parse a base-32 representation of a hash code. */
66 Hash
parseHash32(HashType ht
, const string
& s
);
68 /* Parse a base-16 or base-32 representation of a hash code. */
69 Hash
parseHash16or32(HashType ht
, const string
& s
);
71 /* Verify that the given string is a valid hash code. */
72 bool isHash(const string
& s
);
74 /* Compute the hash of the given string. */
75 Hash
hashString(HashType ht
, const string
& s
);
77 /* Compute the hash of the given file. */
78 Hash
hashFile(HashType ht
, const Path
& path
);
80 /* Compute the hash of the given path. The hash is defined as
81 (essentially) hashString(ht, dumpPath(path)). */
83 extern PathFilter defaultPathFilter
;
84 typedef std::pair
<Hash
, unsigned long long> HashResult
;
85 HashResult
hashPath(HashType ht
, const Path
& path
,
86 PathFilter
& filter
= defaultPathFilter
);
88 /* Compress a hash to the specified number of bytes by cyclically
89 XORing bytes together. */
90 Hash
compressHash(const Hash
& hash
, unsigned int newSize
);
92 /* Parse a string representing a hash type. */
93 HashType
parseHashType(const string
& s
);
95 /* And the reverse. */
96 string
printHashType(HashType ht
);
101 class HashSink
: public BufferedSink
106 unsigned long long bytes
;
109 HashSink(HashType ht
);
110 HashSink(const HashSink
& h
);
112 void write(const unsigned char * data
, size_t len
);
114 HashResult
currentHash();