1717
1818package org .apache .commons .codec .digest ;
1919
20+ import java .nio .charset .Charset ;
21+ import java .nio .charset .StandardCharsets ;
22+
2023/**
2124 * Implementation of the MurmurHash3 32-bit and 128-bit hash functions.
2225 *
23- * <p>MurmurHash is a non-cryptographic hash function suitable for general
24- * hash-based lookup. The name comes from two basic operations, multiply (MU)
25- * and rotate (R), used in its inner loop. Unlike cryptographic hash functions,
26- * it is not specifically designed to be difficult to reverse by an adversary,
27- * making it unsuitable for cryptographic purposes. </p>
26+ * <p>
27+ * MurmurHash is a non-cryptographic hash function suitable for general hash -based lookup. The name comes from two basic
28+ * operations, multiply (MU) and rotate (R), used in its inner loop. Unlike cryptographic hash functions, it is not
29+ * specifically designed to be difficult to reverse by an adversary, making it unsuitable for cryptographic purposes.
30+ * </p>
2831 *
29- * <p>This contains a Java port of the 32-bit hash function {@code MurmurHash3_x86_32}
30- * and the 128-bit hash function {@code MurmurHash3_x64_128} from Austin Applyby's
31- * original {@code c++} code in SMHasher.</p>
32+ * <p>
33+ * This contains a Java port of the 32-bit hash function {@code MurmurHash3_x86_32} and the 128-bit hash function
34+ * {@code MurmurHash3_x64_128} from Austin Applyby's original {@code c++} code in SMHasher.
35+ * </p>
3236 *
33- * <p>This is public domain code with no copyrights. From home page of
34- * <a href="https://github.com/aappleby/smhasher">SMHasher</a>:</p>
37+ * <p>
38+ * This is public domain code with no copyrights. From home page of
39+ * <a href="https://github.com/aappleby/smhasher">SMHasher</a>:
40+ * </p>
3541 *
36- * <blockquote>
37- * "All MurmurHash versions are public domain software, and the author
38- * disclaims all copyright to their code."
39- * </blockquote>
42+ * <blockquote> "All MurmurHash versions are public domain software, and the author disclaims all copyright to their
43+ * code." </blockquote>
4044 *
41- * <p>Original adaption from Apache Hive. That adaption contains a {@code hash64} method
42- * that is not part of the original MurmurHash3 code. It is not recommended to use these methods.
43- * They will be removed in a future release. To obtain a 64-bit hash use half of the bits
44- * from the {@code hash128x64} methods using the input data converted to bytes.<p>
45+ * <p>
46+ * Original adaption from Apache Hive. That adaption contains a {@code hash64} method that is not part of the original
47+ * MurmurHash3 code. It is not recommended to use these methods. They will be removed in a future release. To obtain a
48+ * 64-bit hash use half of the bits from the {@code hash128x64} methods using the input data converted to bytes.
49+ * <p>
4550 *
4651 * @see <a href="https://en.wikipedia.org/wiki/MurmurHash">MurmurHash</a>
47- * @see <a href="https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp">
48- * Original MurmurHash3 c++ code</a>
49- * @see <a href="https://github.com/apache/hive/blob/master/storage-api/src/java/org/apache/hive/common/util/Murmur3.java">
50- * Apache Hive Murmer3</a>
52+ * @see <a href="https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp"> Original MurmurHash3 c++
53+ * code</a>
54+ * @see <a href=
55+ * "https://github.com/apache/hive/blob/master/storage-api/src/java/org/apache/hive/common/util/Murmur3.java">
56+ * Apache Hive Murmer3</a>
5157 * @since 1.13
5258 */
5359public final class MurmurHash3 {
5460
61+ /**
62+ * Default Charset used to convert strings into bytes.
63+ *
64+ * Consider private; package private for tests only.
65+ */
66+ static final Charset GET_BYTES_CHARSET = StandardCharsets .UTF_8 ;
67+
5568 /**
5669 * A random number to use for a hash code.
5770 *
@@ -230,7 +243,7 @@ public static int hash32(final byte[] data) {
230243 * <pre>
231244 * int offset = 0;
232245 * int seed = 104729;
233- * byte[] bytes = data.getBytes();
246+ * byte[] bytes = data.getBytes(StandardCharsets.UTF_8 );
234247 * int hash = MurmurHash3.hash32(bytes, offset, bytes.length, seed);
235248 * </pre>
236249 *
@@ -246,7 +259,7 @@ public static int hash32(final byte[] data) {
246259 */
247260 @ Deprecated
248261 public static int hash32 (final String data ) {
249- final byte [] bytes = data .getBytes ();
262+ final byte [] bytes = data .getBytes (GET_BYTES_CHARSET );
250263 return hash32 (bytes , 0 , bytes .length , DEFAULT_SEED );
251264 }
252265
@@ -748,7 +761,7 @@ public static long[] hash128x64(final byte[] data) {
748761 * <pre>
749762 * int offset = 0;
750763 * int seed = 104729;
751- * byte[] bytes = data.getBytes();
764+ * byte[] bytes = data.getBytes(StandardCharsets.UTF_8 );
752765 * int hash = MurmurHash3.hash128(bytes, offset, bytes.length, seed);
753766 * </pre>
754767 *
@@ -763,7 +776,7 @@ public static long[] hash128x64(final byte[] data) {
763776 */
764777 @ Deprecated
765778 public static long [] hash128 (final String data ) {
766- final byte [] bytes = data .getBytes ();
779+ final byte [] bytes = data .getBytes (GET_BYTES_CHARSET );
767780 return hash128 (bytes , 0 , bytes .length , DEFAULT_SEED );
768781 }
769782
0 commit comments