aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_hash.c
diff options
context:
space:
mode:
authorxrs <xrs@mail36.net>2020-06-17 22:07:39 +0200
committerxrs <xrs@mail36.net>2020-06-17 22:07:39 +0200
commit49cf7a8e893eaf7682ac12c7d0ea5ca4a6d1a73d (patch)
tree71830c1751e291e45795958cad15e9dfa8239e1a /src/util/crypto_hash.c
parentb9333fef25b57bdd7f556f5fb73f9abaef9bc5ef (diff)
parente500e9ec3678dfbb666d173854c134ac3858f8b1 (diff)
downloadgnunet-49cf7a8e893eaf7682ac12c7d0ea5ca4a6d1a73d.tar.gz
gnunet-49cf7a8e893eaf7682ac12c7d0ea5ca4a6d1a73d.zip
Merge branch 'master' into rewrite_of_cadet_test
Diffstat (limited to 'src/util/crypto_hash.c')
-rw-r--r--src/util/crypto_hash.c27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c
index c41c419ff..622953476 100644
--- a/src/util/crypto_hash.c
+++ b/src/util/crypto_hash.c
@@ -244,18 +244,35 @@ GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc,
244/** 244/**
245 * Obtain a bit from a hashcode. 245 * Obtain a bit from a hashcode.
246 * @param code the GNUNET_CRYPTO_hash to index bit-wise 246 * @param code the GNUNET_CRYPTO_hash to index bit-wise
247 * @param bit index into the hashcode, [0...511] 247 * @param bit index into the hashcode, [0...511] where 0 is the leftmost bit
248 * (bytes in code interpreted big endian)
248 * @return Bit \a bit from hashcode \a code, -1 for invalid index 249 * @return Bit \a bit from hashcode \a code, -1 for invalid index
249 */ 250 */
250int 251int
251GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code, unsigned int 252GNUNET_CRYPTO_hash_get_bit_ltr (const struct GNUNET_HashCode *code,
252 bit) 253 unsigned int bit)
254{
255 GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode));
256 return (((unsigned char *) code)[bit >> 3] & (128 >> (bit & 7))) > 0;
257}
258
259/**
260 * Obtain a bit from a hashcode.
261 * @param code the GNUNET_CRYPTO_hash to index bit-wise
262 * @param bit index into the hashcode, [0...511] where 0 is the rightmost bit
263 * (bytes in code interpreted little endian)
264 * @return Bit \a bit from hashcode \a code, -1 for invalid index
265 */
266int
267GNUNET_CRYPTO_hash_get_bit_rtl (const struct GNUNET_HashCode *code,
268 unsigned int bit)
253{ 269{
254 GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode)); 270 GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode));
255 return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0; 271 return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
256} 272}
257 273
258 274
275
259/** 276/**
260 * Determine how many low order bits match in two 277 * Determine how many low order bits match in two
261 * `struct GNUNET_HashCode`s. i.e. - 010011 and 011111 share 278 * `struct GNUNET_HashCode`s. i.e. - 010011 and 011111 share
@@ -275,8 +292,8 @@ GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first,
275 unsigned int i; 292 unsigned int i;
276 293
277 for (i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++) 294 for (i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++)
278 if (GNUNET_CRYPTO_hash_get_bit (first, i) != 295 if (GNUNET_CRYPTO_hash_get_bit_rtl (first, i) !=
279 GNUNET_CRYPTO_hash_get_bit (second, i)) 296 GNUNET_CRYPTO_hash_get_bit_rtl (second, i))
280 return i; 297 return i;
281 return sizeof(struct GNUNET_HashCode) * 8; 298 return sizeof(struct GNUNET_HashCode) * 8;
282} 299}