Home

Awesome

SpookyHash

SpookyHash is a very fast non cryptographic hash function, designed by Bob Jenkins.

It produces well-distributed 128-bit hash values for byte arrays of any length. It can produce 64-bit and 32-bit hash values too, at the same speed, just use the bottom n bits. Long keys hash in 3 bytes per cycle, short keys take about 1 byte per cycle, and there is a 30 cycle startup cost. Keys can be supplied in fragments. The function allows a 128-bit seed. It's named SpookyHash because it was released on Halloween.

This release of SpookyHash integrates support for big endian platforms and multithreading via context variables.

BranchLinux & OSXWindows
masterBuild StatusBuild status
devBuild StatusBuild status

Why use SpookyHash ?

Build

To build a static and dynamic library, as well as a test binary of SpookyHash on Windows, Linux or Mac OSX,

  1. Download premake 5 and make it available in your path

  2. Run the following from the command line

    git clone https://github.com/k0dai/spookyhash.git
    cd spookyhash/build
    premake5 gmake
    make

or alternatively, on Windows for example :

    premake5.exe vs2017
    msbuild CPUTime.sln

If msbuild.exe is not available in your path, just launch Visual Studio by double clicking on SpookyHash.sln and build the library inside the IDE.

Quick start

#include "spookyhash_api.h"

void hash(void* data, size_t data_length) {
    uint64_t seed1 = 0xabcdef, seed2 = 0xfedcba;
    uint32_t seed3 = 0x12ab;

    // Direct use example
    spookyhash_128(data, data_length, &seed1, &seed2);                  // Initially, seed1 and seed2 are used as seeds for the hash,
                                                                        // on function return they contain the resulting 128-bit hash in two uint64_t parts
    uint64_t hash64 = spookyhash_64(data, data_length, seed1);          // Produce 64-bit hash from seed1
    uint32_t hash32 = spookyhash_32(data, data_length, seed3);          // Produce 32-bit hash from seed3

    // Stream use example
    spookyhash_context context;                                         // Create a context variable
    spookyhash_context_init(&context, seed1, seed2);                    // Initialize the context with seed1 and seed2
    spookyhash_update(&context, data, data_length);                     // Add data to hash, use this function repeatedly
    ...
    spookyhash_final(&context, &seed1, &seed2);                         // seed1 and seed2 now contain the resulting 128-bit hash in two uint64_t parts
}