Class MurmurHash2
- java.lang.Object
-
- org.apache.commons.codec.digest.MurmurHash2
-
public final class MurmurHash2 extends java.lang.Object
Implementation of the MurmurHash2 32-bit and 64-bit hash functions.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 functionMurmurHash64A
from Austin Applyby's originalc++
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:
- MurmurHash, Original MurmurHash2 c++ code
-
-
Constructor Summary
Constructors Modifier Constructor Description private
MurmurHash2()
No instance methods.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static int
getLittleEndianInt(byte[] data, int index)
Gets the little-endian int from 4 bytes starting at the specified index.private static long
getLittleEndianLong(byte[] data, int index)
Gets the little-endian long from 8 bytes starting at the specified index.static 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
hash32(java.lang.String text)
Generates a 32-bit hash from a string with a default seed.static int
hash32(java.lang.String text, int from, int length)
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
hash64(java.lang.String text)
Generates a 64-bit hash from a string with a default seed.static long
hash64(java.lang.String text, int from, int length)
Generates a 64-bit hash from a substring with a default seed value.
-
-
-
Field Detail
-
M32
private static final int M32
- See Also:
- Constant Field Values
-
R32
private static final int R32
- See Also:
- Constant Field Values
-
M64
private static final long M64
- See Also:
- Constant Field Values
-
R64
private static final int R64
- See Also:
- Constant Field Values
-
-
Method Detail
-
hash32
public static int hash32(byte[] data, int length, int seed)
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
public static int hash32(byte[] data, int length)
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(byte[], int, int)
-
hash32
public static int hash32(java.lang.String text)
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(byte[], int, int)
-
hash32
public static int hash32(java.lang.String text, int from, int length)
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:
hash32(byte[], int, int)
-
hash64
public static long hash64(byte[] data, int length, int seed)
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
public static long hash64(byte[] data, int length)
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(byte[], int, int)
-
hash64
public static long hash64(java.lang.String text)
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(byte[], int, int)
-
hash64
public static long hash64(java.lang.String text, int from, int length)
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 The input stringfrom
- The starting indexlength
- The length of the substring- Returns:
- The 64-bit hash
- See Also:
hash64(byte[], int, int)
-
getLittleEndianInt
private static int getLittleEndianInt(byte[] data, int index)
Gets the little-endian int from 4 bytes starting at the specified index.- Parameters:
data
- The dataindex
- The index- Returns:
- The little-endian int
-
getLittleEndianLong
private static long getLittleEndianLong(byte[] data, int index)
Gets the little-endian long from 8 bytes starting at the specified index.- Parameters:
data
- The dataindex
- The index- Returns:
- The little-endian long
-
-