20
20
#include "ccache.h"
22
#include <sys/types.h>
29
22
#define HASH_DELIMITER "\000cCaChE"
31
void hash_buffer(struct mdfour *md, const void *s, size_t len)
25
hash_start(struct mdfour *md)
31
hash_buffer(struct mdfour *md, const void *s, size_t len)
33
33
mdfour_update(md, (unsigned char *)s, len);
36
void hash_start(struct mdfour *md)
36
/* Return the hash result as a hex string. Caller frees. */
38
hash_result(struct mdfour *md)
40
unsigned char sum[16];
42
hash_result_as_bytes(md, sum);
43
return format_hash_as_string(sum, (unsigned) md->totalN);
46
/* return the hash result as 16 binary bytes */
48
hash_result_as_bytes(struct mdfour *md, unsigned char *out)
50
hash_buffer(md, NULL, 0);
51
mdfour_result(md, out);
55
hash_equal(struct mdfour *md1, struct mdfour *md2)
57
unsigned char sum1[16], sum2[16];
58
hash_result_as_bytes(md1, sum1);
59
hash_result_as_bytes(md2, sum2);
60
return memcmp(sum1, sum2, sizeof(sum1)) == 0;
48
70
* information X if CCACHE_A is set and information Y if CCACHE_B is set,
49
71
* there should never be a hash collision risk).
51
void hash_delimiter(struct mdfour *md, const char *type)
74
hash_delimiter(struct mdfour *md, const char *type)
53
76
hash_buffer(md, HASH_DELIMITER, sizeof(HASH_DELIMITER));
54
77
hash_buffer(md, type, strlen(type) + 1); /* Include NUL. */
57
void hash_string(struct mdfour *md, const char *s)
81
hash_string(struct mdfour *md, const char *s)
59
83
hash_buffer(md, s, strlen(s));
62
void hash_int(struct mdfour *md, int x)
87
hash_int(struct mdfour *md, int x)
64
89
hash_buffer(md, (char *)&x, sizeof(x));
68
* Add contents of an open file to the hash. Returns 1 on success, otherwise 0.
93
* Add contents of an open file to the hash. Returns true on success, otherwise
70
int hash_fd(struct mdfour *md, int fd)
97
hash_fd(struct mdfour *md, int fd)
75
while ((n = read(fd, buf, sizeof(buf))) > 0) {
76
hash_buffer(md, buf, n);
102
while ((n = read(fd, buf, sizeof(buf))) != 0) {
103
if (n == -1 && errno != EINTR) {
107
hash_buffer(md, buf, n);
86
* Add contents of a file to the hash. Returns 1 on success, otherwise 0.
114
* Add contents of a file to the hash. Returns true on success, otherwise
88
int hash_file(struct mdfour *md, const char *fname)
118
hash_file(struct mdfour *md, const char *fname)
93
123
fd = open(fname, O_RDONLY|O_BINARY);
98
128
ret = hash_fd(md, fd);
103
/* Return the hash result as a hex string. Caller frees. */
104
char *hash_result(struct mdfour *md)
106
unsigned char sum[16];
108
hash_result_as_bytes(md, sum);
109
return format_hash_as_string(sum, (unsigned) md->totalN);
112
/* return the hash result as 16 binary bytes */
113
void hash_result_as_bytes(struct mdfour *md, unsigned char *out)
115
hash_buffer(md, NULL, 0);
116
mdfour_result(md, out);