nvflare.fuel.utils.hash_utils module

class UniformHash(num_buckets: int)[source]

Bases: object

A hash algorithm with uniform distribution. It achieves this with following steps, 1. Get a hash value of the key using first 8 bytes of SHA256 2. Map the hash value to a virtual hash table using modulo operation 3. Map the virtual bucket to real bucket by even distribution

The virtual hash is needed because real hash table size may not be a prime number and there are known issues of modulo operation.

Initialize the hash function :param num_buckets: Number of buckets, e.g. Number of servers to distribute the load

get_num_buckets() int[source]

Get the number of buckets :returns: Number of buckets

hash(key: str) int[source]

Hash the key to a bucket index :param key: A string key to be hashed

Returns:

The bucket index between 0 and num_buckets-1