From a5c4023f9ed6389060f7ac94da9fc6e2f7f233cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Sat, 22 Nov 2014 22:32:16 +0100 Subject: [PATCH] Added twc_hash_tox_id. --- src/twc-utils.c | 15 +++++++++++++++ src/twc-utils.h | 3 +++ 2 files changed, 18 insertions(+) diff --git a/src/twc-utils.c b/src/twc-utils.c index 3f995ce..2ab5fac 100644 --- a/src/twc-utils.c +++ b/src/twc-utils.c @@ -168,3 +168,18 @@ twc_uint32_reverse_bytes(uint32_t num) return res; } + +/** + * Hash a Tox ID of size TOX_CLIENT_ID_SIZE bytes using a modified djb2 hash. + */ +unsigned long long +twc_hash_tox_id(const uint8_t *tox_id) +{ + unsigned long long hash = 5381; + + for (size_t i = 0; i < TOX_CLIENT_ID_SIZE; ++i) + hash ^= (hash << 5) + (hash >> 2) + tox_id[i]; + + return hash; +} + diff --git a/src/twc-utils.h b/src/twc-utils.h index 56e975f..45f5808 100644 --- a/src/twc-utils.h +++ b/src/twc-utils.h @@ -51,5 +51,8 @@ twc_get_friend_id_short(Tox *tox, int32_t friend_number); uint32_t twc_uint32_reverse_bytes(uint32_t num); +unsigned long long +twc_hash_tox_id(const uint8_t *tox_id); + #endif // TOX_WEECHAT_UTILS_H