From fe43907c45344d500907e593f3132db71f7e4a58 Mon Sep 17 00:00:00 2001 From: "Nathan S. Evans" Date: Tue, 21 Sep 2010 12:55:24 +0000 Subject: additional useful hash function --- src/util/crypto_hash.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/util') diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c index af492e15e..0fb2451b2 100644 --- a/src/util/crypto_hash.c +++ b/src/util/crypto_hash.c @@ -756,6 +756,28 @@ GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit) return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0; } +/** + * Determine how many low order bits match in two + * GNUNET_HashCodes. i.e. - 010011 and 011111 share + * the first two lowest order bits, and therefore the + * return value is two (NOT XOR distance, nor how many + * bits match absolutely!). + * + * @param first the first hashcode + * @param second the hashcode to compare first to + * + * @return the number of bits that match + */ +unsigned int GNUNET_CRYPTO_hash_matching_bits(const GNUNET_HashCode *first, const GNUNET_HashCode *second) +{ + unsigned int i; + + for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++) + if (GNUNET_CRYPTO_hash_get_bit (first, i) != GNUNET_CRYPTO_hash_get_bit (second, i)) + return i; + return sizeof (GNUNET_HashCode) * 8; +} + /** * Compare function for HashCodes, producing a total ordering * of all hashcodes. -- cgit v1.2.3