Class ChaChaDataContentBuilder
- All Implemented Interfaces:
DataContentBuilder<DataContent>
InputStream is
consumed.
Variants
The builder selects between two cipher variants:- Stream (ChaCha20): chosen by default or when
initialCounter(int)orwithCounter(int)is used. - AEAD (ChaCha20-Poly1305): chosen when
withAad(byte[])is provided.
IllegalStateException.
Key material
A secret key is resolved in the following order:- Explicit key via
withKey(javax.crypto.SecretKey). - Key generation via
generateKey()with a default 256-bit spec. - Key import via
importKeyRaw(byte[]),importKeyHex(String), orimportKeyBase64(String). - Fallback to default 256-bit generation if none of the above is provided.
Context and headers
A runtimeCtxInterface can be
attached via context(conflux.CtxInterface) to exchange parameters
such as nonce, AAD, or counter. When a context is present, the builder writes
values under algorithm-specific keys from ConfluxKeys. If a header is
requested with withHeader() or a custom SymmetricHeaderCodec
is supplied via withHeaderCodec(SymmetricHeaderCodec), a context is
required; if none was provided, a temporary one is created internally. A
default header codec is chosen based on the variant when no custom codec is
set. Encryption generates a 12-byte nonce internally after claiming its
single-use context. Headerless decryption may receive a nonce through the
decryption-only configuration method.
Usage examples
// 1) ChaCha20 stream encryption with generated key and header
DataContent enc = ChaChaDataContentBuilder.builder()
.generateKey()
.withHeader()
.build(true);
// 2) Headerless ChaCha20-Poly1305 decryption with supplied key, nonce, and AAD
DataContent dec = ChaChaDataContentBuilder.builder()
.withKey(secretKey)
.withDecryptionNonce(nonce12) // 12 bytes
.withAad("meta".getBytes(StandardCharsets.UTF_8))
.build(false);
// 3) ChaCha20 stream encryption with explicit counter propagated via context
DataContent enc2 = ChaChaDataContentBuilder.builder()
.withKey(secretKey)
.context(ctx)
.withCounter(7) // overrides initialCounter in ctx
.build(true);
// 4) Import a key from hex and decrypt without headers
DataContent dec2 = ChaChaDataContentBuilder.builder()
.importKeyHex(hexKey)
.build(false);
Behavior of build(boolean)
Calling build(true)
returns a pipeline that encrypts as the stream is read; build(false)
returns a pipeline that decrypts. In stream mode,
initialCounter(int) sets the spec's initial counter (default is 1),
while withCounter(int) stores an override in the context. In AEAD
mode, withAad(byte[]) supplies additional authenticated data.
Thread-safety
Instances are mutable and not thread-safe. Configure and use each builder instance from a single thread.- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbuild(boolean encrypt) Builds a streaming encryption or decryption pipeline according to the current configuration.static ChaChaDataContentBuilderbuilder(ZeroEchoSession session) Returns a new builder instance for constructing ChaCha20 or ChaCha20-Poly1305 streaming pipelines.context(CtxInterface ctx) Sets an optional runtime context used to read or write parameters such as nonce, AAD, or counter.Requests generation of a fresh ChaCha key using a default 256-bit specification.importKeyBase64(String b64) Imports a secret key from a Base64 string.importKeyHex(String hex) Imports a secret key from a hexadecimal string.importKeyRaw(byte[] raw) Imports a secret key from raw key bytes appropriate for the selected ChaCha variant.initialCounter(int c) Sets the initial counter for ChaCha20 stream mode.withAad(byte[] aad) Sets additional authenticated data for ChaCha20-Poly1305.withCounter(int c) Sets an explicit counter override to be propagated through the context.withDecryptionNonce(byte[] nonce) Sets an out-of-band nonce for headerless decryption.Requests that a header be emitted (on encrypt) or parsed (on decrypt).Sets a custom header codec to emit or parse algorithm headers.Sets an explicit secret key to use for encryption or decryption.
-
Method Details
-
builder
Returns a new builder instance for constructing ChaCha20 or ChaCha20-Poly1305 streaming pipelines.Examples
DataContent enc = ChaChaDataContentBuilder.builder() .generateKey() .withHeader() .build(true);- Parameters:
session- active session used to create cryptographic contexts- Returns:
- a new
ChaChaDataContentBuilder
-
withKey
Sets an explicit secret key to use for encryption or decryption.Supplying a key clears any previously configured key generation or import specification.
- Parameters:
k- the secret key to use; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifkis null
-
generateKey
Requests generation of a fresh ChaCha key using a default 256-bit specification.Enabling key generation clears any previously configured explicit key or import specification.
- Returns:
thisbuilder for chaining
-
importKeyRaw
Imports a secret key from raw key bytes appropriate for the selected ChaCha variant.Supplying an import spec clears any previously configured explicit key or key generation request.
- Parameters:
raw- the raw key bytes; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifrawis nullIllegalArgumentException- ifrawis not a valid key length
-
importKeyHex
Imports a secret key from a hexadecimal string.Supplying an import spec clears any previously configured explicit key or key generation request.
- Parameters:
hex- the hex-encoded key; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifhexis nullIllegalArgumentException- ifhexis not valid hexadecimal or not a valid key length
-
importKeyBase64
Imports a secret key from a Base64 string.Supplying an import spec clears any previously configured explicit key or key generation request.
- Parameters:
b64- the Base64-encoded key; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifb64is nullIllegalArgumentException- ifb64is not valid Base64 or not a valid key length
-
context
Sets an optional runtime context used to read or write parameters such as nonce, AAD, or counter.When a header is requested and no context is provided, a temporary context is created internally.
- Parameters:
ctx- the context instance to use; may be null- Returns:
thisbuilder for chaining
-
withDecryptionNonce
Sets an out-of-band nonce for headerless decryption.For ChaCha20 and ChaCha20-Poly1305 this must be 12 bytes. The value is rejected when building an encrypting pipeline because encryption always generates its nonce internally.
- Parameters:
nonce- the nonce bytes; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifnonceisnull
-
initialCounter
Sets the initial counter for ChaCha20 stream mode.Calling this method selects stream mode unless AEAD is selected via
withAad(byte[]). The default initial counter is 1.- Parameters:
c- the initial counter value- Returns:
thisbuilder for chaining
-
withCounter
Sets an explicit counter override to be propagated through the context.This value is stored under an algorithm-specific key in the provided context and overrides
initialCounter(int)if present.- Parameters:
c- the counter value to use- Returns:
thisbuilder for chaining
-
withAad
Sets additional authenticated data for ChaCha20-Poly1305.Providing AAD selects AEAD mode. Passing
nullor an empty array leaves the decision to other configuration such as stream counters.- Parameters:
aad- the AAD bytes; may be null- Returns:
thisbuilder for chaining
-
withHeaderCodec
Sets a custom header codec to emit or parse algorithm headers.Supplying a codec clears the implicit header request flag. If no codec is supplied but a header is requested via
withHeader(), a default is chosen based on the selected variant.- Parameters:
codec- the header codec to use; may be null to clear- Returns:
thisbuilder for chaining
-
withHeader
Requests that a header be emitted (on encrypt) or parsed (on decrypt).If no custom codec is set, a default codec is chosen depending on whether stream or AEAD mode is inferred.
- Returns:
thisbuilder for chaining
-
build
Builds a streaming encryption or decryption pipeline according to the current configuration.AEAD mode is selected if AAD is provided via
withAad(byte[]), using the algorithm id "CHACHA20-POLY1305". Otherwise stream mode is used, with the algorithm id "CHACHA20".The key is resolved in this order: explicit key, key generation spec, key import spec, default 256-bit generation.
If a header is requested and no context is provided, a temporary context is created. Encryption generates its nonce internally; a configured decryption nonce is rejected for encrypting pipelines.
DataContent enc = ChaChaDataContentBuilder.builder() .generateKey() .withHeader() .build(true);- Specified by:
buildin interfaceDataContentBuilder<DataContent>- Parameters:
encrypt-trueto build an encrypting pipeline,falsefor a decrypting pipeline- Returns:
- a
DataContentinstance that performs the requested operation when its stream is consumed - Throws:
IllegalStateException- if conflicting configuration is detected or key construction fails
-