diff --git a/src/main/java/org/apache/commons/codec/digest/Blake3.java b/src/main/java/org/apache/commons/codec/digest/Blake3.java index cc0838069f..72c8b30269 100644 --- a/src/main/java/org/apache/commons/codec/digest/Blake3.java +++ b/src/main/java/org/apache/commons/codec/digest/Blake3.java @@ -119,19 +119,22 @@ private Blake3(final int[] key, final int flags) { /** * Resets this instance back to its initial state when it was first constructed. + * @return this */ - public void reset() { + public Blake3 reset() { engineState.reset(); + return this; } /** * Updates this hash state using the provided bytes. * * @param in source array to update data from + * @return this * @throws NullPointerException if in is null */ - public void update(final byte[] in) { - update(in, 0, in.length); + public Blake3 update(final byte[] in) { + return update(in, 0, in.length); } /** @@ -140,13 +143,15 @@ public void update(final byte[] in) { * @param in source array to update data from * @param offset where in the array to begin reading bytes * @param length number of bytes to update + * @return this * @throws NullPointerException if in is null * @throws IndexOutOfBoundsException if offset or length are negative or if offset + length is greater than the * length of the provided array */ - public void update(final byte[] in, final int offset, final int length) { + public Blake3 update(final byte[] in, final int offset, final int length) { checkBufferArgs(in, offset, length); engineState.inputData(in, offset, length); + return this; } /** @@ -154,10 +159,11 @@ public void update(final byte[] in, final int offset, final int length) { * previously finalized bytes. Note that this can finalize up to 264 bytes per instance. * * @param out destination array to finalize bytes into + * @return this * @throws NullPointerException if out is null */ - public void doFinalize(final byte[] out) { - doFinalize(out, 0, out.length); + public Blake3 doFinalize(final byte[] out) { + return doFinalize(out, 0, out.length); } /** @@ -167,13 +173,15 @@ public void doFinalize(final byte[] out) { * @param out destination array to finalize bytes into * @param offset where in the array to begin writing bytes to * @param length number of bytes to finalize + * @return this * @throws NullPointerException if out is null * @throws IndexOutOfBoundsException if offset or length are negative or if offset + length is greater than the * length of the provided array */ - public void doFinalize(final byte[] out, final int offset, final int length) { + public Blake3 doFinalize(final byte[] out, final int offset, final int length) { checkBufferArgs(out, offset, length); engineState.outputHash(out, offset, length); + return this; } /** @@ -244,9 +252,7 @@ public static Blake3 initKeyDerivationFunction(final byte[] kdfContext) { * @throws NullPointerException if data is null */ public static byte[] hash(final byte[] data) { - final Blake3 blake3 = Blake3.initHash(); - blake3.update(data); - return blake3.doFinalize(OUT_LEN); + return Blake3.initHash().update(data).doFinalize(OUT_LEN); } /** @@ -258,9 +264,7 @@ public static byte[] hash(final byte[] data) { * @throws NullPointerException if key or data are null */ public static byte[] keyedHash(final byte[] key, final byte[] data) { - final Blake3 blake3 = Blake3.initKeyedHash(key); - blake3.update(data); - return blake3.doFinalize(OUT_LEN); + return Blake3.initKeyedHash(key).update(data).doFinalize(OUT_LEN); } private static void checkBufferArgs(final byte[] buffer, final int offset, final int length) { diff --git a/src/test/java/org/apache/commons/codec/digest/Blake3TestVectorsTest.java b/src/test/java/org/apache/commons/codec/digest/Blake3TestVectorsTest.java index 1e5eeb10ba..97f4d49e12 100644 --- a/src/test/java/org/apache/commons/codec/digest/Blake3TestVectorsTest.java +++ b/src/test/java/org/apache/commons/codec/digest/Blake3TestVectorsTest.java @@ -281,8 +281,7 @@ private void initData(final int inputLength, final String hash, final String key @MethodSource("data") public void hashArbitraryOutputLength(final int inputLength, final String hash, final String keyedHash, final String deriveKey) throws DecoderException { initData(inputLength, hash, keyedHash, deriveKey); - hasher.update(inputByteArray); - final byte[] actual = hasher.doFinalize(hashByteArray.length); + final byte[] actual = hasher.update(inputByteArray).doFinalize(hashByteArray.length); assertArrayEquals(hashByteArray, actual); } @@ -298,8 +297,7 @@ public void hashTruncatedOutput(final int inputLength, final String hash, final @MethodSource("data") public void keyedHashArbitraryOutputLength(final int inputLength, final String hash, final String keyedHash, final String deriveKey) throws DecoderException { initData(inputLength, hash, keyedHash, deriveKey); - keyedHasher.update(inputByteArray); - final byte[] actual = keyedHasher.doFinalize(keyedHashByteArray.length); + final byte[] actual = keyedHasher.update(inputByteArray).doFinalize(keyedHashByteArray.length); assertArrayEquals(keyedHashByteArray, actual); } @@ -315,11 +313,9 @@ public void keyedHashTruncatedOutput(final int inputLength, final String hash, f @MethodSource("data") public void keyDerivation(final int inputLength, final String hash, final String keyedHash, final String deriveKey) throws DecoderException { initData(inputLength, hash, keyedHash, deriveKey); - kdfHasher.update(inputByteArray); - final byte[] actual = kdfHasher.doFinalize(deriveKeyByteArray.length); + final byte[] actual = kdfHasher.update(inputByteArray).doFinalize(deriveKeyByteArray.length); assertArrayEquals(deriveKeyByteArray, actual); - kdfHasher.reset(); - kdfHasher.update(inputByteArray); + kdfHasher.reset().update(inputByteArray); final byte[] truncated = kdfHasher.doFinalize(32); assertArrayEquals(Arrays.copyOf(deriveKeyByteArray, 32), truncated); }