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