Class Blake3
Hashing
Hash mode calculates the same output hash given the same input bytes and can be used as both a message digest and and extensible output function.
Blake3 hasher = Blake3.initHash();
hasher.update("Hello, world!".getBytes(StandardCharsets.UTF_8));
byte[] hash = new byte[32];
hasher.doFinalize(hash);
Keyed Hashing
Keyed hashes take a 32-byte secret key and calculates a message authentication code on some input bytes. These also work as pseudo-random functions (PRFs) with extensible output similar to the extensible hash output. Note that Blake3 keyed hashes have the same performance as plain hashes; the key is used in initialization in place of a standard initialization vector used for plain hashing.
SecureRandom random = SecureRandom.getInstanceStrong();
byte[] key = new byte[32];
random.nextBytes(key);
Blake3 hasher = Blake3.initKeyedHash(key);
hasher.update("Hello, Alice!".getBytes(StandardCharsets.UTF_8));
byte[] mac = new byte[32];
hasher.doFinalize(mac);
Key Derivation
A specific hash mode for deriving session keys and other derived keys in a unique key derivation context identified by some sequence of bytes. These context strings should be unique but do not need to be kept secret. Additional input data is hashed for key material which can be finalized to derive subkeys. To derive multiple subkeys, request their combined length in one finalization and split the output. Repeated finalizations start at the beginning of the same output and do not derive new subkeys.
String context = "org.apache.commons.codec.digest.Blake3Example";
byte[] sharedSecret = ...;
byte[] senderId = ...;
byte[] recipientId = ...;
Blake3 kdf = Blake3.initKeyDerivationFunction(context.getBytes(StandardCharsets.UTF_8));
kdf.update(sharedSecret);
kdf.update(senderId);
kdf.update(recipientId);
byte[] keys = kdf.doFinalize(64);
byte[] txKey = Arrays.copyOfRange(keys, 0, 32);
byte[] rxKey = Arrays.copyOfRange(keys, 32, 64);
Adapted from the ISC-licensed O(1) Cryptography library by Matt Sicker and ported from the reference public domain implementation by Jack O'Connor.
- Since:
- 1.16
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptiondoFinalize(byte[] out) Finalizes hash output into the provided array.doFinalize(byte[] out, int offset, int length) Finalizes hash output into a region of the provided array.byte[]doFinalize(int nrBytes) Finalizes hash output into a new array.static byte[]hash(byte[] data) Calculates the Blake3 hash of the provided data.static Blake3initHash()Constructs a fresh Blake3 hash function.static Blake3initKeyDerivationFunction(byte[] kdfContext) Constructs a fresh Blake3 key derivation function using the provided key derivation context byte string.static Blake3initKeyedHash(byte[] key) Constructs a fresh Blake3 keyed hash function.static byte[]keyedHash(byte[] key, byte[] data) Calculates the Blake3 keyed hash (MAC) of the provided data.reset()Resets this instance back to its initial state when it was first constructed.update(byte[] in) Updates this hash state using the provided bytes.update(byte[] in, int offset, int length) Updates this hash state using the provided bytes at an offset.
-
Method Details
-
hash
Calculates the Blake3 hash of the provided data.- Parameters:
data- source array to absorb data from.- Returns:
- 32-byte hash squeezed from the provided data.
- Throws:
NullPointerException- Thrown if data is null.
-
initHash
-
initKeyDerivationFunction
Constructs a fresh Blake3 key derivation function using the provided key derivation context byte string. The instance returned functions as a key-derivation function which can further absorb additional context data before squeezing derived key data.- Parameters:
kdfContext- A globally unique key-derivation context byte string to separate key derivation contexts from each other.- Returns:
- fresh Blake3 instance in key derivation mode.
- Throws:
NullPointerException- Thrown if kdfContext is null.
-
initKeyedHash
Constructs a fresh Blake3 keyed hash function. The instance returned functions as a pseudorandom function (PRF) or as a message authentication code (MAC).- Parameters:
key- 32-byte secret key.- Returns:
- fresh Blake3 instance in keyed mode using the provided key.
- Throws:
NullPointerException- Thrown if key is null.IllegalArgumentException- Thrown if key is not 32 bytes.
-
keyedHash
Calculates the Blake3 keyed hash (MAC) of the provided data.- Parameters:
key- 32-byte secret key.data- source array to absorb data from.- Returns:
- 32-byte mac squeezed from the provided data.
- Throws:
NullPointerException- Thrown if key or data are null.
-
doFinalize
Finalizes hash output into the provided array.This method does not change the hash state. Each invocation starts at the beginning of the output for the bytes supplied to
update(). Repeated invocations without additional input produce the same output prefix. Additional calls toupdate()append input to the existing hash state, even after finalization.- Parameters:
out- destination array to finalize bytes into.- Returns:
thisinstance.- Throws:
NullPointerException- Thrown if out is null.
-
doFinalize
Finalizes hash output into a region of the provided array.This method does not change the hash state. Each invocation starts at the beginning of the output for the bytes supplied to
update(). Repeated invocations without additional input produce the same output prefix. The offset selects the destination array position, not a position in the hash output. Additional calls toupdate()append input to the existing hash state, even after finalization.- Parameters:
out- destination array to finalize bytes into.offset- where in the array to begin writing bytes to.length- number of bytes to finalize.- Returns:
thisinstance.- Throws:
NullPointerException- Thrown if out is null.IndexOutOfBoundsException- Thrown if offset or length are negative or if offset + length is greater than the length of the provided array.
-
doFinalize
Finalizes hash output into a new array.This method does not change the hash state. Each invocation starts at the beginning of the output for the bytes supplied to
update(). Repeated invocations without additional input produce the same output prefix. Additional calls toupdate()append input to the existing hash state, even after finalization.- Parameters:
nrBytes- number of bytes to finalize.- Returns:
- requested number of finalized bytes.
- Throws:
IllegalArgumentException- Thrown if nrBytes is negative.
-
reset
-
update
Updates this hash state using the provided bytes.Input is appended to the existing hash state, including after finalization. Call
reset()first to start a new message.- Parameters:
in- source array to update data from.- Returns:
thisinstance.- Throws:
NullPointerException- Thrown if in is null.
-
update
Updates this hash state using the provided bytes at an offset.Input is appended to the existing hash state, including after finalization. Call
reset()first to start a new message.- Parameters:
in- source array to update data from.offset- where in the array to begin reading bytes.length- number of bytes to update.- Returns:
thisinstance.- Throws:
NullPointerException- Thrown if in is null.IndexOutOfBoundsException- Thrown if offset or length are negative or if offset + length is greater than the length of the provided array.
-