2 * Copyright (C) 2002 Andrew Tridgell
3 * Copyright (C) 2010 Joel Rosdahl
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 3 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #define HASH_DELIMITER "\000cCaChE"
25 hash_start(struct mdfour *md)
31 hash_buffer(struct mdfour *md, const void *s, size_t len)
33 mdfour_update(md, (unsigned char *)s, len);
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;
64 * Hash some data that is unlikely to occur in the input. The idea is twofold:
66 * - Delimit things like arguments from each other (e.g., so that -I -O2 and
67 * -I-O2 hash differently).
68 * - Tag different types of hashed information so that it's possible to do
69 * conditional hashing of information in a safe way (e.g., if we want to hash
70 * information X if CCACHE_A is set and information Y if CCACHE_B is set,
71 * there should never be a hash collision risk).
74 hash_delimiter(struct mdfour *md, const char *type)
76 hash_buffer(md, HASH_DELIMITER, sizeof(HASH_DELIMITER));
77 hash_buffer(md, type, strlen(type) + 1); /* Include NUL. */
81 hash_string(struct mdfour *md, const char *s)
83 hash_buffer(md, s, strlen(s));
87 hash_int(struct mdfour *md, int x)
89 hash_buffer(md, (char *)&x, sizeof(x));
93 * Add contents of an open file to the hash. Returns true on success, otherwise
97 hash_fd(struct mdfour *md, int fd)
102 while ((n = read(fd, buf, sizeof(buf))) != 0) {
103 if (n == -1 && errno != EINTR) {
107 hash_buffer(md, buf, n);
114 * Add contents of a file to the hash. Returns true on success, otherwise
118 hash_file(struct mdfour *md, const char *fname)
123 fd = open(fname, O_RDONLY|O_BINARY);
128 ret = hash_fd(md, fd);