aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNathan S. Evans <evans@in.tum.de>2010-09-21 12:55:24 +0000
committerNathan S. Evans <evans@in.tum.de>2010-09-21 12:55:24 +0000
commitfe43907c45344d500907e593f3132db71f7e4a58 (patch)
treee6c473892b6b0cf9873ed326c773aed4e7f2e4ec /src
parentf27f0f6afa53f94878f18bef86a5f96f8aae13c4 (diff)
downloadgnunet-fe43907c45344d500907e593f3132db71f7e4a58.tar.gz
gnunet-fe43907c45344d500907e593f3132db71f7e4a58.zip
additional useful hash function
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_crypto_lib.h14
-rw-r--r--src/util/crypto_hash.c22
2 files changed, 36 insertions, 0 deletions
diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h
index 0b250311a..8a8e71a96 100644
--- a/src/include/gnunet_crypto_lib.h
+++ b/src/include/gnunet_crypto_lib.h
@@ -484,6 +484,20 @@ void GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
484int GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, 484int GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code,
485 unsigned int bit); 485 unsigned int bit);
486 486
487/**
488 * Determine how many low order bits match in two
489 * GNUNET_HashCodes. i.e. - 010011 and 011111 share
490 * the first two lowest order bits, and therefore the
491 * return value is two (NOT XOR distance, nor how many
492 * bits match absolutely!).
493 *
494 * @param first the first hashcode
495 * @param second the hashcode to compare first to
496 *
497 * @return the number of bits that match
498 */
499unsigned int GNUNET_CRYPTO_hash_matching_bits(const GNUNET_HashCode *first, const GNUNET_HashCode *second);
500
487 501
488/** 502/**
489 * Compare function for HashCodes, producing a total ordering 503 * Compare function for HashCodes, producing a total ordering
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
@@ -757,6 +757,28 @@ GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit)
757} 757}
758 758
759/** 759/**
760 * Determine how many low order bits match in two
761 * GNUNET_HashCodes. i.e. - 010011 and 011111 share
762 * the first two lowest order bits, and therefore the
763 * return value is two (NOT XOR distance, nor how many
764 * bits match absolutely!).
765 *
766 * @param first the first hashcode
767 * @param second the hashcode to compare first to
768 *
769 * @return the number of bits that match
770 */
771unsigned int GNUNET_CRYPTO_hash_matching_bits(const GNUNET_HashCode *first, const GNUNET_HashCode *second)
772{
773 unsigned int i;
774
775 for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++)
776 if (GNUNET_CRYPTO_hash_get_bit (first, i) != GNUNET_CRYPTO_hash_get_bit (second, i))
777 return i;
778 return sizeof (GNUNET_HashCode) * 8;
779}
780
781/**
760 * Compare function for HashCodes, producing a total ordering 782 * Compare function for HashCodes, producing a total ordering
761 * of all hashcodes. 783 * of all hashcodes.
762 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2. 784 * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.