/k-hash

A simple single header 64 and 32 bit hash function using only add, sub, ror, and xor.

Primary LanguageCMIT LicenseMIT

K-HASH

A simple single header 64 bit hash function using only add, sub, ror, and xor. This a just general-purpose hash function for like making hash maps and similar data-structures. It's is not a cryptographic hash function!

It is quite easy to choose a new function at runtime by just passing a random 64 bit value to the func parameter such as:

#include "khash.h"

void foo() {
    /*
    code ....
    */
    uint64_t hash = khash64_fn(value, your_random_number);
    /*
    code ....
    */
}

K-HASH 64 Output

Here is the output of the 64 bit hash of the integers [0, 259199] using 0x1dcedff1a8b17e89 as the function.

drawing

K-HASH 32 Output

Here is the output of the 32 bit hash of the integers [0, 518399] using 0x6bb75f13 and 0xf9e5a345 as the function;

drawing

The output of the above images was generated by basically doing the following for a hash.

for(int i = 0; i < sizeof(hash_bytes); i++) {
    pixel[img_offset + i].r = hash_bytes[i];
    pixel[img_offset + i].g = hash_bytes[i];
    pixel[img_offset + i].b = hash_bytes[i];
    pixel[img_offset + i].a = 255;
}