Class MurmurHash2
MurmurHash is a non-cryptographic hash function suitable for general hash-based lookup. The name comes from two basic operations, multiply (MU) and rotate (R), used in its inner loop. Unlike cryptographic hash functions, it is not specifically designed to be difficult to reverse by an adversary, making it unsuitable for cryptographic purposes.
This contains a Java port of the 32-bit hash function MurmurHash2
and the 64-bit hash function MurmurHash64A
from Austin Appleby's
original c++
code in SMHasher.
This is a re-implementation of the original C code plus some additional features.
This is public domain code with no copyrights. From home page of SMHasher:
"All MurmurHash versions are public domain software, and the author disclaims all copyright to their code."
- Since:
- 1.13
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
hash32
(byte[] data, int length) Generates a 32-bit hash from byte array with the given length and a default seed value.static int
hash32
(byte[] data, int length, int seed) Generates a 32-bit hash from byte array with the given length and seed.static int
Generates a 32-bit hash from a string with a default seed.static int
Generates a 32-bit hash from a substring with a default seed value.static long
hash64
(byte[] data, int length) Generates a 64-bit hash from byte array with given length and a default seed value.static long
hash64
(byte[] data, int length, int seed) Generates a 64-bit hash from byte array of the given length and seed.static long
Generates a 64-bit hash from a string with a default seed.static long
Generates a 64-bit hash from a substring with a default seed value.
-
Method Details
-
hash32
Generates a 32-bit hash from byte array with the given length and a default seed value. This is a helper method that will produce the same result as:int seed = 0x9747b28c; int hash = MurmurHash2.hash32(data, length, seed);
- Parameters:
data
- The input byte arraylength
- The length of the array- Returns:
- The 32-bit hash
- See Also:
-
hash32
Generates a 32-bit hash from byte array with the given length and seed.- Parameters:
data
- The input byte arraylength
- The length of the arrayseed
- The initial seed value- Returns:
- The 32-bit hash
-
hash32
Generates a 32-bit hash from a string with a default seed.Before 1.14 the string was converted using default encoding. Since 1.14 the string is converted to bytes using UTF-8 encoding.
This is a helper method that will produce the same result as:int seed = 0x9747b28c; byte[] bytes = data.getBytes(StandardCharsets.UTF_8); int hash = MurmurHash2.hash32(bytes, bytes.length, seed);
- Parameters:
text
- The input string- Returns:
- The 32-bit hash
- See Also:
-
hash32
Generates a 32-bit hash from a substring with a default seed value. The string is converted to bytes using the default encoding. This is a helper method that will produce the same result as:int seed = 0x9747b28c; byte[] bytes = text.substring(from, from + length).getBytes(StandardCharsets.UTF_8); int hash = MurmurHash2.hash32(bytes, bytes.length, seed);
- Parameters:
text
- The input stringfrom
- The starting indexlength
- The length of the substring- Returns:
- The 32-bit hash
- See Also:
-
hash64
Generates a 64-bit hash from byte array with given length and a default seed value. This is a helper method that will produce the same result as:int seed = 0xe17a1465; int hash = MurmurHash2.hash64(data, length, seed);
- Parameters:
data
- The input byte arraylength
- The length of the array- Returns:
- The 64-bit hash
- See Also:
-
hash64
Generates a 64-bit hash from byte array of the given length and seed.- Parameters:
data
- The input byte arraylength
- The length of the arrayseed
- The initial seed value- Returns:
- The 64-bit hash of the given array
-
hash64
Generates a 64-bit hash from a string with a default seed.Before 1.14 the string was converted using default encoding. Since 1.14 the string is converted to bytes using UTF-8 encoding.
This is a helper method that will produce the same result as:
int seed = 0xe17a1465; byte[] bytes = data.getBytes(StandardCharsets.UTF_8); int hash = MurmurHash2.hash64(bytes, bytes.length, seed);
- Parameters:
text
- The input string- Returns:
- The 64-bit hash
- See Also:
-
hash64
Generates a 64-bit hash from a substring with a default seed value. The string is converted to bytes using the default encoding. This is a helper method that will produce the same result as:int seed = 0xe17a1465; byte[] bytes = text.substring(from, from + length).getBytes(StandardCharsets.UTF_8); int hash = MurmurHash2.hash64(bytes, bytes.length, seed);
- Parameters:
text
- The input stringfrom
- The starting indexlength
- The length of the substring- Returns:
- The 64-bit hash
- See Also:
-