You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/17 20:11:29 UTC
[commons-codec] branch master updated: Make new API Fluent
This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-codec.git
The following commit(s) were added to refs/heads/master by this push:
new cc2808b6 Make new API Fluent
cc2808b6 is described below
commit cc2808b67cdc9a14fc17f913495dd28bd248c8b3
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 17 16:11:24 2023 -0400
Make new API Fluent
---
.../org/apache/commons/codec/digest/Blake3.java | 30 ++++++++++++----------
.../codec/digest/Blake3TestVectorsTest.java | 12 +++------
2 files changed, 21 insertions(+), 21 deletions(-)
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 cc083806..72c8b302 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 @@ public final class Blake3 {
/**
* 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 final class Blake3 {
* @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 final class Blake3 {
* previously finalized bytes. Note that this can finalize up to 2<sup>64</sup> 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 final class Blake3 {
* @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 final class Blake3 {
* @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 final class Blake3 {
* @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 1e5eeb10..97f4d49e 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 @@ public class Blake3TestVectorsTest {
@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 class Blake3TestVectorsTest {
@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 class Blake3TestVectorsTest {
@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);
}