Skip to content

Commit

Permalink
Make new API Fluent
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Jun 17, 2023
1 parent 0901537 commit cc2808b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
30 changes: 17 additions & 13 deletions src/main/java/org/apache/commons/codec/digest/Blake3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -140,24 +143,27 @@ 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;
}

/**
* Finalizes hash output data that depends on the sequence of updated bytes preceding this invocation and any
* 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);
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down

0 comments on commit cc2808b

Please sign in to comment.