This is a version of Bob Jenkins' SpookyHash hashing algorithm that can be evaluated at compile time. The Hash128, Hash64, and Hash32 functions are now all constexpr and can be used for things like switch case statements. This is still a C++ implementation.
CTSpookyHash's output is identical to the original SpookyHash's, and performance is comparable when used in run-time situations. Just like the original SpookyHash, this code is in the public domain and can be used unconditionally.
- Download
CTSpookyHash.handCTSpookyHash.cpp. - Add them to your project.
- Use
CTSpookyHash::Hash128,CTSpookyHash::Hash64orCTSpookyHash::Hash32in your code. - Profit.
- This is an implementation of SpookyHash V2.
- C++11 is needed for
constexpr. - Just like the original SpookyHash, this code is not portable to big-endian processors.
- SpookyHash is not cryptographic: do not use it in security scenarios.
- Turning off
ALLOW_UNALIGNED_READSwill make all functions run-time only. - Only messages that are smaller than 192 bytes (
sc_bufSize) can be evaluated at compile time. Larger messages must be hashed at run-time. - The functions
CTSpookyHash::Init,CTSpookyHash::UpdateandCTSpookyHash::Finalare still run-time only.
Bob Jenkins' original version of SpookyHash is available at http://burtleburtle.net/bob/hash/spooky.html.