Skip to content

Commit 627b5fd

Browse files
committed
Merge branch 'master' into release
2 parents 2b0fecf + f40005a commit 627b5fd

11 files changed

Lines changed: 90 additions & 60 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ The <action> type attribute can be add,update,fix,remove.
5555
<action issue="CODEC-273" dev="ggregory" type="add" due-to="Gary Gregory">Add Path APIs to org.apache.commons.codec.digest.DigestUtils similar to File APIs.</action>
5656
<action issue="CODEC-274" dev="ggregory" type="add" due-to="Gary Gregory">Add SHA-512/224 and SHA-512/256 to DigestUtils for Java 9 and up.</action>
5757
<action issue="CODEC-275" dev="ggregory" type="add" due-to="Claude Warren">Add missing note in javadoc when sign extension error is present #34.</action>
58+
<action issue="CODEC-276" dev="ggregory" type="add" due-to="Gary Gregory">Reliance on default encoding in MurmurHash2 and MurmurHash3.</action>
5859
</release>
5960

6061
<release version="1.13" date="2019-07-20" description="Feature and fix release.">

src/main/java/org/apache/commons/codec/binary/Base32.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,16 @@ public Base32(final int lineLength, final byte[] lineSeparator, final boolean us
338338
* silently ignored, but has implications for other bytes, too. This method subscribes to the garbage-in,
339339
* garbage-out philosophy: it will not check the provided data for validity.
340340
* </p>
341+
* <p>
342+
* Output is written to {@link org.apache.commons.codec.binary.BaseNCodec.Context#buffer Context#buffer} as 8-bit
343+
* octets, using {@link org.apache.commons.codec.binary.BaseNCodec.Context#pos Context#pos} as the buffer position
344+
* </p>
341345
*
342-
* @param in
343-
* byte[] array of ascii data to Base32 decode.
344-
* @param inPos
345-
* Position to start reading data from.
346-
* @param inAvail
347-
* Amount of bytes available from input for decoding.
346+
* @param in byte[] array of ascii data to Base32 decode.
347+
* @param inPos Position to start reading data from.
348+
* @param inAvail Amount of bytes available from input for decoding.
348349
* @param context the context to be used
349350
*
350-
* Output is written to {@link org.apache.commons.codec.binary.BaseNCodec.Context#buffer Context#buffer}
351-
* as 8-bit octets, using {@link org.apache.commons.codec.binary.BaseNCodec.Context#pos Context#pos} as the buffer position
352351
*/
353352
@Override
354353
void decode(final byte[] in, int inPos, final int inAvail, final Context context) {

src/main/java/org/apache/commons/codec/binary/BaseNCodecOutputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class BaseNCodecOutputStream extends FilterOutputStream {
4949

5050
/**
5151
* TODO should this be protected?
52-
*
52+
*
5353
* @param out the underlying output or null.
5454
* @param basedCodec a BaseNCodec.
5555
* @param doEncode true to encode, false to decode, TODO should be an enum?

src/main/java/org/apache/commons/codec/digest/DigestUtils.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ public static byte[] digest(final MessageDigest messageDigest, final InputStream
134134
* On error reading from the stream
135135
* @since 1.14
136136
*/
137-
public static byte[] digest(final MessageDigest messageDigest, final Path data, final OpenOption... options) throws IOException {
137+
public static byte[] digest(final MessageDigest messageDigest, final Path data, final OpenOption... options)
138+
throws IOException {
138139
return updateDigest(messageDigest, data, options).digest();
139140
}
140141

@@ -1195,7 +1196,7 @@ public static String sha512_224Hex(final byte[] data) {
11951196
* @param data
11961197
* Data to digest
11971198
* @return SHA-512/224 digest as a hex string
1198-
* @throws IOException
1199+
* @throws IOException
11991200
* On error reading from the stream
12001201
* @since 1.14
12011202
*/
@@ -1271,7 +1272,7 @@ public static String sha512_256Hex(final byte[] data) {
12711272
* @param data
12721273
* Data to digest
12731274
* @return SHA-512/256 digest as a hex string
1274-
* @throws IOException
1275+
* @throws IOException
12751276
* On error reading from the stream
12761277
* @since 1.14
12771278
*/
@@ -1452,7 +1453,8 @@ private static MessageDigest updateDigest(final MessageDigest digest, final File
14521453
* On error reading from the stream
14531454
* @since 1.8
14541455
*/
1455-
public static MessageDigest updateDigest(final MessageDigest digest, final InputStream inputStream) throws IOException {
1456+
public static MessageDigest updateDigest(final MessageDigest digest, final InputStream inputStream)
1457+
throws IOException {
14561458
final byte[] buffer = new byte[STREAM_BUFFER_LENGTH];
14571459
int read = inputStream.read(buffer, 0, STREAM_BUFFER_LENGTH);
14581460

@@ -1478,13 +1480,13 @@ public static MessageDigest updateDigest(final MessageDigest digest, final Input
14781480
* On error reading from the stream
14791481
* @since 1.14
14801482
*/
1481-
public static MessageDigest updateDigest(final MessageDigest digest, final Path path, final OpenOption... options) throws IOException {
1483+
public static MessageDigest updateDigest(final MessageDigest digest, final Path path, final OpenOption... options)
1484+
throws IOException {
14821485
try (final BufferedInputStream inputStream = new BufferedInputStream(Files.newInputStream(path, options))) {
14831486
return updateDigest(digest, inputStream);
14841487
}
14851488
}
14861489

1487-
14881490
/**
14891491
* Reads through a RandomAccessFile and updates the digest for the data using non-blocking-io (NIO)
14901492
*

src/main/java/org/apache/commons/codec/digest/MessageDigestAlgorithms.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class MessageDigestAlgorithms {
9898
* <p>
9999
* Included starting in Oracle Java 9.
100100
* </p>
101-
*
101+
*
102102
* @since 1.14
103103
*/
104104
public static final String SHA_512_224 = "SHA-512/224";
@@ -108,7 +108,7 @@ public class MessageDigestAlgorithms {
108108
* <p>
109109
* Included starting in Oracle Java 9.
110110
* </p>
111-
*
111+
*
112112
* @since 1.14
113113
*/
114114
public static final String SHA_512_256 = "SHA-512/256";

src/main/java/org/apache/commons/codec/digest/MurmurHash2.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.apache.commons.codec.digest;
1919

20+
import java.nio.charset.Charset;
21+
import java.nio.charset.StandardCharsets;
22+
2023
/**
2124
* Implementation of the MurmurHash2 32-bit and 64-bit hash functions.
2225
*
@@ -48,6 +51,13 @@
4851
*/
4952
public final class MurmurHash2 {
5053

54+
/**
55+
* Default Charset used to convert strings into bytes.
56+
*
57+
* Consider private; package private for tests only.
58+
*/
59+
static final Charset GET_BYTES_CHARSET = StandardCharsets.UTF_8;
60+
5161
// Constants for 32-bit variant
5262
private static final int M32 = 0x5bd1e995;
5363
private static final int R32 = 24;
@@ -132,7 +142,7 @@ public static int hash32(final byte[] data, final int length) {
132142
*
133143
* <pre>
134144
* int seed = 0x9747b28c;
135-
* byte[] bytes = data.getBytes();
145+
* byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
136146
* int hash = MurmurHash2.hash32(bytes, bytes.length, seed);
137147
* </pre>
138148
*
@@ -141,7 +151,7 @@ public static int hash32(final byte[] data, final int length) {
141151
* @see #hash32(byte[], int, int)
142152
*/
143153
public static int hash32(final String text) {
144-
final byte[] bytes = text.getBytes();
154+
final byte[] bytes = text.getBytes(GET_BYTES_CHARSET);
145155
return hash32(bytes, bytes.length);
146156
}
147157

@@ -152,7 +162,7 @@ public static int hash32(final String text) {
152162
*
153163
* <pre>
154164
* int seed = 0x9747b28c;
155-
* byte[] bytes = text.substring(from, from + length).getBytes();
165+
* byte[] bytes = text.substring(from, from + length).getBytes(StandardCharsets.UTF_8);
156166
* int hash = MurmurHash2.hash32(bytes, bytes.length, seed);
157167
* </pre>
158168
*
@@ -243,7 +253,7 @@ public static long hash64(final byte[] data, final int length) {
243253
*
244254
* <pre>
245255
* int seed = 0xe17a1465;
246-
* byte[] bytes = data.getBytes();
256+
* byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
247257
* int hash = MurmurHash2.hash64(bytes, bytes.length, seed);
248258
* </pre>
249259
*
@@ -252,7 +262,7 @@ public static long hash64(final byte[] data, final int length) {
252262
* @see #hash64(byte[], int, int)
253263
*/
254264
public static long hash64(final String text) {
255-
final byte[] bytes = text.getBytes();
265+
final byte[] bytes = text.getBytes(GET_BYTES_CHARSET);
256266
return hash64(bytes, bytes.length);
257267
}
258268

@@ -263,7 +273,7 @@ public static long hash64(final String text) {
263273
*
264274
* <pre>
265275
* int seed = 0xe17a1465;
266-
* byte[] bytes = text.substring(from, from + length).getBytes();
276+
* byte[] bytes = text.substring(from, from + length).getBytes(StandardCharsets.UTF_8);
267277
* int hash = MurmurHash2.hash64(bytes, bytes.length, seed);
268278
* </pre>
269279
*

src/main/java/org/apache/commons/codec/digest/MurmurHash3.java

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,54 @@
1717

1818
package 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
*/
5359
public 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

src/main/java/org/apache/commons/codec/digest/XXHash32.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@
2424
/**
2525
* Implementation of the xxhash32 hash algorithm.
2626
*
27-
* <p>Copied from Commons Compress 1.14
28-
* <a href="https://git-wip-us.apache.org/repos/asf?p=commons-compress.git;a=blob;f=src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java;h=a406ffc197449be594d46f0d2712b2d4786a1e68;hb=HEAD">https://git-wip-us.apache.org/repos/asf?p=commons-compress.git;a=blob;f=src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java;h=a406ffc197449be594d46f0d2712b2d4786a1e68;hb=HEAD</a></p>
29-
* <p>NotThreadSafe</p>
27+
* <p>
28+
* Copied from Commons Compress 1.14 <a href=
29+
* "https://git-wip-us.apache.org/repos/asf?p=commons-compress.git;a=blob;f=src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java;h=a406ffc197449be594d46f0d2712b2d4786a1e68;hb=HEAD">https://git-wip-us.apache.org/repos/asf?p=commons-compress.git;a=blob;f=src/main/java/org/apache/commons/compress/compressors/lz4/XXHash32.java;h=a406ffc197449be594d46f0d2712b2d4786a1e68;hb=HEAD</a>
30+
* </p>
31+
* <p>
32+
* NotThreadSafe
33+
* </p>
34+
*
3035
* @see <a href="http://cyan4973.github.io/xxHash/">xxHash</a>
3136
* @since 1.11
3237
*/

src/main/java/org/apache/commons/codec/language/ColognePhonetic.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ public CologneOutputBuffer(final int buffSize) {
241241
* '0' is only stored if it is the first entry.
242242
* Ignored chars are never stored.
243243
* If the code is the same as the last code (whether stored or not) it is not stored.
244-
*
245-
* @param code the code to store.
244+
*
245+
* @param code the code to store.
246246
*/
247247
public void put(final char code) {
248248
if (code != CHAR_IGNORE && lastCode != code && (code != '0' || length == 0)) {
@@ -404,7 +404,7 @@ public String encode(final String text) {
404404

405405
/**
406406
* Compares the first encoded string to the second encoded string.
407-
*
407+
*
408408
* @param text1 source text to encode before testing for equality.
409409
* @param text2 source text to encode before testing for equality.
410410
* @return {@code true} if the encoding the first string equals the encoding of the second string, {@code false}

src/test/java/org/apache/commons/codec/digest/DigestUtilsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void testMd2Length() {
198198
hash = DigestUtils.md2(getBytesUtf8(hashMe));
199199
assertEquals(16, hash.length);
200200
}
201-
201+
202202
@Test
203203
public void testMd5Hex() throws IOException {
204204
// Examples from RFC 1321

0 commit comments

Comments
 (0)