Class MurmurHash2

java.lang.Object
org.apache.commons.codec.digest.MurmurHash2

public final class MurmurHash2 extends Object
Implements 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 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 Type
    Method
    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(String text)
    Generates a 32-bit hash from a string with a default seed.
    static int
    hash32(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(String text)
    Generates a 64-bit hash from a string with a default seed.
    static long
    hash64(String text, int from, int length)
    Generates a 64-bit hash from a substring with a default seed value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • 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 array
      length - The length of the array
      Returns:
      The 32-bit hash
      See Also:
    • 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 array
      length - The length of the array
      seed - The initial seed value
      Returns:
      The 32-bit hash
    • hash32

      public static int hash32(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

      public static int hash32(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 string
      from - The starting index
      length - The length of the substring
      Returns:
      The 32-bit hash
      See Also:
    • 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 array
      length - The length of the array
      Returns:
      The 64-bit hash
      See Also:
    • 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 array
      length - The length of the array
      seed - The initial seed value
      Returns:
      The 64-bit hash of the given array
    • hash64

      public static long hash64(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

      public static long hash64(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 input string
      from - The starting index
      length - The length of the substring
      Returns:
      The 64-bit hash
      See Also: