public final class MurmurHash2
extends java.lang.Object
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 Applyby'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."
Modifier and Type | Method and Description |
---|---|
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.
|
public static int hash32(byte[] data, int length, int seed)
data
- The input byte arraylength
- The length of the arrayseed
- The initial seed valuepublic static int hash32(byte[] data, int length)
int seed = 0x9747b28c; int hash = MurmurHash2.hash32(data, length, seed);
data
- The input byte arraylength
- The length of the arrayhash32(byte[], int, int)
public static int hash32(java.lang.String text)
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);
text
- The input stringhash32(byte[], int, int)
public static int hash32(java.lang.String text, int from, int length)
int seed = 0x9747b28c; byte[] bytes = text.substring(from, from + length).getBytes(StandardCharsets.UTF_8); int hash = MurmurHash2.hash32(bytes, bytes.length, seed);
text
- The input stringfrom
- The starting indexlength
- The length of the substringhash32(byte[], int, int)
public static long hash64(byte[] data, int length, int seed)
data
- The input byte arraylength
- The length of the arrayseed
- The initial seed valuepublic static long hash64(byte[] data, int length)
int seed = 0xe17a1465; int hash = MurmurHash2.hash64(data, length, seed);
data
- The input byte arraylength
- The length of the arrayhash64(byte[], int, int)
public static long hash64(java.lang.String text)
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);
text
- The input stringhash64(byte[], int, int)
public static long hash64(java.lang.String text, int from, int length)
int seed = 0xe17a1465; byte[] bytes = text.substring(from, from + length).getBytes(StandardCharsets.UTF_8); int hash = MurmurHash2.hash64(bytes, bytes.length, seed);
text
- The The input stringfrom
- The starting indexlength
- The length of the substringhash64(byte[], int, int)
Copyright © 2010 - 2020 Adobe. All Rights Reserved