Avoid double hashing in HashMap?

62 Views Asked by At

My keys are already the (blake3) hashes of my values.

How can avoid wasteful re-hashing of these keys when storing the values in a HashMap?

1

There are 1 best solutions below

0
bk2204 On BEST ANSWER

You can define a custom instance of Hasher which uses your BLAKE3 values, but you'll need to reduce them down to a u64, since that's the type that's used by the trait. You can pick the first 64 bits or XOR the output together to produce a value.

However, be careful with this, because the default Rust hash table uses a secret key which randomizes the values to prevent attackers from providing many keys that all hash to the same bucket, causing pathological performance. If your data are untrusted, then it's a security problem (DoS vulnerability) to use an unkeyed hash, even one that's cryptographically secure like BLAKE3. Note that we assume that the attacker knows how your hash scheme works (because usually it's pretty easy to figure it out), so that isn't a valid defense to this sort of vulnerability.