Skip to content

Commit 1490fbf

Browse files
committed
[CODEC-269] Allow repeat calls to IncrementalHash32.end().
Repeat calls with no additional data should create the same hash.
1 parent 2d744c8 commit 1490fbf

3 files changed

Lines changed: 12 additions & 5 deletions

File tree

src/changes/changes.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ The <action> type attribute can be add,update,fix,remove.
4242
</properties>
4343
<body>
4444

45+
<release version="1.14" date="TBD" description="Feature and fix release.">
46+
<action issue="CODEC-269" dev="aherbert" type="fix">Allow repeat calls to IncrementalHash32.end() to generate the same value.</action>
47+
</release>
48+
4549
<release version="1.13" date="2019-07-20" description="Feature and fix release.">
46-
<action issue="CODEC-255" dev="sebb" due-to="Holger Grote" type="fix">ColognePhonetic handles x incorrectly</action>
47-
<action issue="CODEC-254" dev="sebb" due-to="Holger Grote" type="fix">ColognePhonetic does not treat the letter H correctly</action>
50+
<action issue="CODEC-255" dev="sebb" due-to="Holger Grote" type="fix">ColognePhonetic handles x incorrectly</action>
51+
<action issue="CODEC-254" dev="sebb" due-to="Holger Grote" type="fix">ColognePhonetic does not treat the letter H correctly</action>
4852
<action issue="CODEC-134" dev="tmousaw-ptc" type="fix">Reject any decode request for a value that is impossible to encode to for Base32/Base64 rather than blindly decoding.</action>
4953
<action issue="CODEC-236" dev="melloware" due-to="Viliam Holub" type="add">MurmurHash2 for 32-bit or 64-bit value.</action>
5054
<action issue="CODEC-236" dev="melloware" due-to="Austin Appleby" type="add">MurmurHash3 for 32-bit or 128-bit value.</action>

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,8 @@ public final void add(final byte[] data, final int offset, final int length) {
923923
* @return The 32-bit hash
924924
*/
925925
public final int end() {
926+
// Allow calling end() again after adding no data to return the same result.
927+
int result = hash;
926928
// ************
927929
// Note: This fails to apply masking using 0xff to the 3 remaining bytes.
928930
// ************
@@ -939,12 +941,12 @@ public final int end() {
939941
k1 *= C1_32;
940942
k1 = Integer.rotateLeft(k1, R1_32);
941943
k1 *= C2_32;
942-
hash ^= k1;
944+
result ^= k1;
943945
}
944946

945947
// finalization
946-
hash ^= totalLen;
947-
return fmix32(hash);
948+
result ^= totalLen;
949+
return fmix32(result);
948950
}
949951

950952
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ private static void assertIncrementalHash32(byte[] bytes, int seed, int... block
597597
offset += block;
598598
final int h2 = inc.end();
599599
Assert.assertEquals("Hashes differ", h1, h2);
600+
Assert.assertEquals("Hashes differ after no additional data", h1, inc.end());
600601
}
601602
}
602603
}

0 commit comments

Comments
 (0)