2 * Copyright (C) 2007-2009 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file hash.h Inline functions for hash values. */
9 #include "portable_io.h"
11 /** hash arrays are always unsigned char. */
12 #define HASH_TYPE unsigned char
14 /** Size of the hash value in bytes. */
17 void sha1_hash(const char *data, unsigned long len, unsigned char *sha1);
19 /** Our own sha1 implementation, see sha1.c. */
20 #define hash_function sha1_hash
25 * \param h1 Pointer to the first hash value.
26 * \param h2 Pointer to the second hash value.
28 * \return 1, -1, or zero, depending on whether \a h1 is greater than,
29 * less than or equal to h2, respectively.
31 _static_inline_ int hash_compare(HASH_TYPE *h1, HASH_TYPE *h2)
35 for (i = 0; i < HASH_SIZE; i++) {
45 * Convert a hash value to ascii format.
47 * \param hash the hash value.
48 * \param asc Result pointer.
50 * \a asc must point to an area of at least 2 * \p HASH_SIZE + 1 bytes which
51 * will be filled by the function with the ascii representation of the hash
52 * value given by \a hash, and a terminating \p NULL byte.
54 _static_inline_ void hash_to_asc(HASH_TYPE *hash, char *asc)
57 const char hexchar[] = "0123456789abcdef";
59 for (i = 0; i < HASH_SIZE; i++) {
60 asc[2 * i] = hexchar[hash[i] >> 4];
61 asc[2 * i + 1] = hexchar[hash[i] & 0xf];
63 asc[2 * HASH_SIZE] = '\0';