xxHash is an extremely fast Hash algorithm, running at RAM speed limits. It also successfully passes all tests from the SMHasher suite.
A 64-bit version, named XXH64, is available since r35. It offers much better speed, but for 64-bit applications only.
Streaming functions generate the xxHash value from an incremental input. This method is slower than single-call functions, due to state management. For
small inputs, prefer 32 and 64, which are better optimized.
XXH state must first be allocated, using 32_createState.
Start a new hash by initializing state with a seed, using 32_reset.
Then, feed the hash state by calling 32_update as many times as necessary. Obviously, input must be allocated and read accessible. The function
returns an error code, with 0 meaning OK, and any other value meaning there is an error.
Finally, a hash value can be produced anytime, by using 32_digest. This function returns the 32-bits hash as an int.
It's still possible to continue inserting input into the hash state after a digest, and generate some new hash values later on, by calling again
32_digest.
When done, release the state, using 32_freeState.
Example code for incrementally hashing a file:
#include <stdio.h>
#include <xxhash.h>
#define BUFFER_SIZE 256
// Note: XXH64 and XXH3 use the same interface.
XXH32_hash_t
hashFile(FILE* stream)
{
XXH32_state_t* state;
unsigned char buf[BUFFER_SIZE];
size_t amt;
XXH32_hash_t hash;
state = XXH32_createState(); // Create a state
assert(state != NULL); // Error check here
XXH32_reset(state, 0xbaad5eed); // Reset state with our seed
while ((amt = fread(buf, 1, sizeof(buf), stream)) != 0) {
XXH32_update(state, buf, amt); // Hash the file in chunks
}
hash = XXH32_digest(state); // Finalize the hash
XXH32_freeState(state); // Clean up
return hash;
}
The default return values from XXH functions are unsigned 32 and 64 bit integers. This the simplest and fastest format for further post-processing.
However, this leaves open the question of what is the order on the byte level, since little and big endian conventions will store the same number differently.
The canonical representation settles this issue by mandating big-endian convention, the same convention as human-readable numbers (large digits first).
When writing hash values to storage, sending them over a network, or printing them, it's highly recommended to use the canonical representation to ensure portability across a wider range of systems, present and future.
XXH3 is a more recent hash algorithm featuring:
Speed analysis methodology is explained here:
https://fastcompression.blogspot.com/2019/03/presenting-xxh3.html
Compared to XXH64, expect XXH3 to run approximately ~2x faster on large inputs and >3x faster on small ones, exact differences vary depending on platform.
XXH3's speed benefits greatly from SIMD and 64-bit arithmetic, but does not require it. Most 32-bit and 64-bit targets that can run XXH32 smoothly can run XXH3 at competitive speeds, even without vector support. Further details are explained in the implementation.
Optimized implementations are provided for AVX512, AVX2, SSE2, NEON, POWER8, ZVector and scalar targets. This can be controlled via the XXH_VECTOR macro.
XXH3 implementation is portable:
XXH3 offers 2 variants, _64bits and _128bits. When only 64 bits are needed, prefer invoking the _64bits variant, as it reduces
the amount of mixing, resulting in faster speed on small inputs. It's also generally simpler to manipulate a scalar return type than a struct.
The API supports one-shot hashing, streaming mode, and custom secrets.
These variants generate hash values using either seed for "short" keys (< XXH3_MIDSIZE_MAX = 240 bytes) or secret for
"large" keys (≥ XXH3_MIDSIZE_MAX).
This generally benefits speed, compared to _withSeed() or _withSecret(). _withSeed() has to generate the secret on the fly for
"large" keys. It's fast, but can be perceptible for "not so large" keys (< 1 KB). _withSecret() has to generate the masks on the fly for
"small" keys, which requires more instructions than _withSeed() variants. Therefore, _withSecretandSeed variant combines the best of
both worlds.
When secret has been generated by 3_generateSecret_fromSeed, this variant produces exactly the same results as _withSeed()`
variant, hence offering only a pure speed benefit on "large" input, by skipping the need to regenerate the secret for every large input.
Another usage scenario is to hash the secret to a 64-bit hash value, for example with 3_64bits, which then becomes the seed, and then employ both
the seed and the secret in _withSecretandSeed(). On top of speed, an added benefit is that each bit in the secret has a 50% chance to swap each
bit in the output, via its impact to the seed. This is not guaranteed when using the secret directly in "small data" scenarios, because only portions
of the secret are employed for small data.
| Modifier and Type | Field and Description |
|---|---|
| public static final int | XXH3_INTERNALBUFFER_SIZE
The size of the internal XXH3 buffer. |
| public static final int | XXH3_SECRET_DEFAULT_SIZE
Default size of the secret buffer (and |
| public static final int | XXH3_SECRET_SIZE_MIN
The bare minimum size for a custom secret. |
| public static final int | XXH_ERROR
Exit code for the streaming API. |
| public static final int | XXH_OK
Exit code for the streaming API. |
| public static final int | XXH_VERSION_MAJOR
The major version number. |
| public static final int | XXH_VERSION_MINOR
The minor version number. |
| public static final int | XXH_VERSION_NUMBER
The version number |
| public static final int | XXH_VERSION_RELEASE
The release version number. |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public static native void | |
| public static native void | |
| public static native int | |
| public static native void | |
| public static native int | |
| public static native int | |
| public static native void | |
| public static native void | |
| public static native long | |
| public static native int | |
| public static native int | |
| public static native int | |
| public static native int | |
| public static native int | nXXH32_update(long statePtr, long input, long
the length of length)input, in bytesUnsafe version of: |
| public static native void | |
| public static native void | |
| public static native int | |
| public static native int | nXXH3_128bits_reset_withSecret(long statePtr, long secret, long secretSize)
Unsafe version of: |
| public static native int | |
| public static native int | nXXH3_128bits_reset_withSeed(long statePtr, long seed)
Unsafe version of: |
| public static native int | nXXH3_128bits_update(long statePtr, long input, long
the length of length)input, in bytesUnsafe version of: |
| public static native void | nXXH3_128bits_withSecret(long data, long len, long secret, long secretSize, long __result)
Unsafe version of: |
| public static native void | nXXH3_128bits_withSecretandSeed(long input, long length, long secret, long secretSize, long seed, long __result)
|
| public static native void | nXXH3_128bits_withSeed(long data, long len, long seed, long __result)
Unsafe version of: |
| public static native long | |
| public static native long | |
| public static native int | |
| public static native int | nXXH3_64bits_reset_withSecret(long statePtr, long secret, long secretSize)
Unsafe version of: |
| public static native int | |
| public static native int | |
| public static native int | nXXH3_64bits_update(long statePtr, long input, long
the length of length)input, in bytesUnsafe version of: |
| public static native long | nXXH3_64bits_withSecret(long data, long len, long secret, long secretSize)
Unsafe version of: |
| public static native long | |
| public static native long | |
| public static native void | |
| public static native long | |
| public static native int | |
| public static native int | nXXH3_generateSecret(long secretBuffer, long secretSize, long customSeed, long customSeedSize)
Unsafe version of: |
| public static native void | nXXH3_generateSecret_fromSeed(long secretBuffer, long seed)
Unsafe version of: |
| public static native void | |
| public static native long | |
| public static native void | |
| public static native void | |
| public static native long | |
| public static native long | |
| public static native int | |
| public static native long | |
| public static native int | |
| public static native int | nXXH64_update(long statePtr, long input, long
the length of length)input, in bytesUnsafe version of: |
| public static XXH128Hash | XXH128(ByteBuffer data, long seed, XXH128Hash __result)
Simple alias to pre-selected |
| public static void | XXH128_canonicalFromHash(XXH128Canonical
the dst, XXH128Hash XXH128_canonical_t pointer to be stored tothe hash)XXH128_hash_t to be convertedConverts an |
| public static int | XXH128_cmp(ByteBuffer h128_1, ByteBuffer h128_2)
This comparator is compatible with stdlib's |
| public static XXH128Hash | XXH128_hashFromCanonical(XXH128Canonical
the src, XXH128Hash __result)XXH128_canonical_t to convertConverts an |
| public static boolean | |
| public static int | Returns: the calculated 32-bit hash valuethe block of data to be hashed, at least input, int length bytes in sizethe 32-bit seed to alter the hash's output predictably seed)Calculates the 32-bit hash of |
| public static void | XXH32_canonicalFromHash(XXH32Canonical
the dst, int XXH32_canonical_t pointer to be stored to.the hash)XXH32_hash_t to be convertedConverts an |
| public static void | XXH32_copyState(XXH32State
the state to copy to dst_state, XXH32State the state to copy from src_state)Copies one |
| public static XXH32State | Returns: an allocatedXXH32_state_t on success, NULL on failureAllocates an |
| public static int | Returns: the calculated xxHash32 value from that statethe state struct to calculate the hash from statePtr)Returns the calculated hash value from an |
| public static int | |
| public static int | XXH32_hashFromCanonical(XXH32Canonical
the src)XXH32_canonical_t to convertConverts an |
| public static int | XXH32_reset(XXH32State
the state struct to reset statePtr, int the 32-bit seed to alter the hash result predictably seed)Resets an |
| public static int | XXH32_update(XXH32State
the state struct to update statePtr, ByteBuffer the block of data to be hashed, at least input)length bytes in sizeConsumes a block of |
| public static XXH128Hash | |
| public static XXH128Hash | XXH3_128bits_digest(XXH3State
the state struct to calculate the hash from statePtr, XXH128Hash __result)Returns the calculated XXH3 128-bit hash value from an |
| public static int | XXH3_128bits_reset(XXH3State
the state struct to reset statePtr)Resets an |
| public static int | XXH3_128bits_reset_withSecret(XXH3State statePtr, ByteBuffer secret)
Custom secret 128-bit variant of XXH3. |
| public static int | |
| public static int | XXH3_128bits_reset_withSeed(XXH3State
the state struct to reset statePtr, long he 64-bit seed to alter the state seed)Resets an |
| public static int | XXH3_128bits_update(XXH3State
the state struct to update statePtr, ByteBuffer the block of data to be hashed, at least input)length bytes in sizeConsumes a block of |
| public static XXH128Hash | XXH3_128bits_withSecret(ByteBuffer data, ByteBuffer secret, XXH128Hash __result)
Custom secret 128-bit variant of XXH3. |
| public static XXH128Hash | |
| public static XXH128Hash | XXH3_128bits_withSeed(ByteBuffer data, long seed, XXH128Hash __result)
Seeded 128-bit variant of XXH3. |
| public static long | |
| public static long | XXH3_64bits_digest(XXH3State
the state struct to calculate the hash from statePtr)Returns the calculated XXH3 64-bit hash value from an |
| public static int | XXH3_64bits_reset(XXH3State
the state struct to reset statePtr)Resets an |
| public static int | XXH3_64bits_reset_withSecret(XXH3State statePtr, ByteBuffer secret)
|
| public static int | |
| public static int | XXH3_64bits_reset_withSeed(XXH3State
the state struct to reset statePtr, long the 64-bit seed to alter the state seed)Resets an |
| public static int | XXH3_64bits_update(XXH3State
the state struct to update statePtr, ByteBuffer the block of data to be hashed, at least input)length bytes in sizeConsumes a block of |
| public static long | XXH3_64bits_withSecret(ByteBuffer data, ByteBuffer secret)
64-bit variant of XXH3 with a custom "secret". |
| public static long | |
| public static long | |
| public static void | |
| public static XXH3State | |
| public static int | |
| public static int | XXH3_generateSecret(ByteBuffer secretBuffer, ByteBuffer customSeed)
Derives a high-entropy secret from any user-defined content, named |
| public static void | XXH3_generateSecret_fromSeed(ByteBuffer secretBuffer, long seed)
Generate the same secret as the |
| public static void | |
| public static long | Returns: the calculated 64-bit hashthe block of data to be hashed, at least input, long length bytes in sizethe 64-bit seed to alter the hash's output predictably seed)Calculates the 64-bit hash of |
| public static void | XXH64_canonicalFromHash(XXH64Canonical
the dst, long XXH64_canonical_t pointer to be stored to.the hash)XXH64_hash_t to be convertedConverts an |
| public static void | XXH64_copyState(XXH64State
the state to copy to dst_state, XXH64State the state to copy from src_state)Copies one |
| public static XXH64State | Returns: an allocatedXXH64_state_t on success, NULL on failureAllocates an |
| public static long | Returns: the calculated xxHash64 value from that statethe state struct to calculate the hash from statePtr)Returns the calculated hash value from an |
| public static int | |
| public static long | XXH64_hashFromCanonical(XXH64Canonical
the src)XXH64_canonical_t to convertConverts an |
| public static int | XXH64_reset(XXH64State
the state struct to reset statePtr, long the 64-bit seed to alter the hash result predictably seed)Resets an |
| public static int | XXH64_update(XXH64State
the state struct to update statePtr, ByteBuffer the block of data to be hashed, at least input)length bytes in sizeConsumes a block of |