Yes I totally invented this term šŸ˜› What I mean by it is producing multiple hashes from a single key. Like this (if the syntax is unfamiliar to you read this):

Or like this (for non-template version which returns a vector):

Why? One place where I needed such sorcery was my bloom filter implementation. The idea is simple: one key, multiple hashes, repeatable (multiple calls with the same key produce the same hashes). But how? STL only comes with one hashing function. True, but it comes with multiple random number generators which can be seeded with a hash!

The solution then is to hash once, seed the random number generator, and make multiple calls to the RNG, like this (hash.hpp):

You can use it like this (multi_hash.cpp):

HashN(‘Vorbrodt’s C++ Blog’):
1977331388
699200791
437177953

HashN(‘Vorbrodt’s C++ Blog’):
1977331388
699200791
437177953

HashN(‘https://vorbrodt.blog’):
1924360287
1619619789
1594567998

HashNT(‘Vorbrodt’s C++ Blog’):
1977331388
699200791
437177953

HashNT(‘Vorbrodt’s C++ Blog’):
1977331388
699200791
437177953

HashNT(‘https://vorbrodt.blog’):
1924360287
1619619789
1594567998

Program output.

8 Replies to “Multi-hashing”

    1. Yea, I’ve been very busy lately, besides… after cranking out 81 posts in such short period of time I honestly burned out a bit; it was a massive brain dump in such short period of time. But I’ll get back to it eventually šŸ™‚

  1. Hi. If you have a hash collision let’s say then you will use the same seed getting all three hash values being exactly the same . Then what’s the point of using multiple hash values for example in the case of a bloom filter? The point of using multiple hash functions is to reduce the amount of simultaneous collisions.

    1. Key is hashed and result used to seed RNG. Thatā€™s so that A) same key will hash to same value each time and B) what is the likelihood that seeded RNG will emit 3 identical values in a row? If you hash something using my code and get three identical values you need to look at the RNG shipped with STL not the algorithm in my code šŸ¤·ā€ā™‚ļø

    2. Perhaps you missed the fact that the hash seeds RNG and the returned hash values are consecutively generated by RNG? Otherwise I donā€™t understand your comment nor how this code could possibly return an array of identical hashes for any given keyā€¦

Leave a Reply