4 #include "serialise.hh"
10 typedef enum { htUnknown
, htMD5
, htSHA1
, htSHA256
} HashType
;
13 const int md5HashSize
= 16;
14 const int sha1HashSize
= 20;
15 const int sha256HashSize
= 32;
17 extern const string base32Chars
;
22 static const unsigned int maxHashSize
= 32;
23 unsigned int hashSize
;
24 unsigned char hash
[maxHashSize
];
28 /* Create an unusable hash object. */
31 /* Create a zero-filled hash object. */
34 /* Check whether two hash are equal. */
35 bool operator == (const Hash
& h2
) const;
37 /* Check whether two hash are not equal. */
38 bool operator != (const Hash
& h2
) const;
41 bool operator < (const Hash
& h
) const;
45 /* Convert a hash to a hexadecimal representation. */
46 string
printHash(const Hash
& hash
);
48 /* Parse a hexadecimal representation of a hash code. */
49 Hash
parseHash(HashType ht
, const string
& s
);
51 /* Returns the length of a base-32 hash representation. */
52 unsigned int hashLength32(const Hash
& hash
);
54 /* Convert a hash to a base-32 representation. */
55 string
printHash32(const Hash
& hash
);
57 /* Print a hash in base-16 if it's MD5, or base-32 otherwise. */
58 string
printHash16or32(const Hash
& hash
);
60 /* Parse a base-32 representation of a hash code. */
61 Hash
parseHash32(HashType ht
, const string
& s
);
63 /* Parse a base-16 or base-32 representation of a hash code. */
64 Hash
parseHash16or32(HashType ht
, const string
& s
);
66 /* Verify that the given string is a valid hash code. */
67 bool isHash(const string
& s
);
69 /* Compute the hash of the given string. */
70 Hash
hashString(HashType ht
, const string
& s
);
72 /* Compute the hash of the given file. */
73 Hash
hashFile(HashType ht
, const Path
& path
);
75 /* Compute the hash of the given path. The hash is defined as
76 (essentially) hashString(ht, dumpPath(path)). */
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
);
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
);
87 /* Parse a string representing a hash type. */
88 HashType
parseHashType(const string
& s
);
90 /* And the reverse. */
91 string
printHashType(HashType ht
);
96 class HashSink
: public BufferedSink
101 unsigned long long bytes
;
104 HashSink(HashType ht
);
105 HashSink(const HashSink
& h
);
107 void write(const unsigned char * data
, size_t len
);
109 HashResult
currentHash();