Class AesDataContentBuilder
- All Implemented Interfaces:
DataContentBuilder<DataContent>
DataContent in pull‑mode.
This is the simplest way to integrate AES into pipelines. Supply the key with
withKey(javax.crypto.SecretKey), choose a mode (e.g.,
modeGcm(int)), optionally install a header codec via
withHeader(), and call build(boolean) for encryption
(true) or decryption (false). Connect your upstream source
with
DataContent.setInput(zeroecho.sdk.content.api.DataContent)
and consume the resulting InputStream.
Examples
// Encrypt (pull-mode)
DataContent enc = AesDataContentBuilder.builder()
.withKey(secret)
.modeGcm(128)
.withHeader()
.build(true);
enc.setInput(PlainFileBuilder.ofPath(inPath).build());
try (InputStream s = enc.getStream()) { s.transferTo(Files.newOutputStream(ctPath)); }
// Decrypt (pull-mode)
DataContent dec = AesDataContentBuilder.builder()
.withKey(secret)
.modeGcm(128)
.withHeader()
.build(false);
dec.setInput(PlainFileBuilder.ofPath(ctPath).build());
try (InputStream s = dec.getStream()) { s.transferTo(Files.newOutputStream(ptPath)); }
Runtime parameters
Optional AAD can be provided viawithAad(byte[]). Encryption always generates its IV internally.
Headerless decryption may receive an out-of-band IV through
withDecryptionIv(byte[]); that configuration is rejected when
building an encrypting pipeline. For GCM, AAD defaults to empty.
Thread‑safety
The builder is not thread‑safe. BuiltDataContent instances are independent.- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionbuild(boolean encrypt) Builds an encrypting or decryptingDataContent.static AesDataContentBuilderbuilder(ZeroEchoSession session) Creates a new AES builder with default spec (GCM/128, no header) and no key selected.context(CtxInterface ctx) Provides a Conflux session context for exchanging IV/AAD/tagBits and enabling header I/O.Returns the generated secret key associated with this instance.generateKey(int bits) Requests generation of a fresh AES key of the given size.importKeyBase64(String b64) Imports a Base64‑encoded AES key (no padding required).importKeyHex(String hex) Imports a hex‑encoded AES key (32/48/64 hex chars).importKeyRaw(byte[] raw) Imports a raw AES key (16/24/32 bytes).Selects CBC/NOPADDING.Selects CBC/PKCS5PADDING.modeCtr()Selects CTR with NOPADDING.modeGcm(int tagBits) Selects GCM and sets the authentication tag length in bits.Copies mode/padding/tagBits/header from a fully builtAesSpec.withAad(byte[] aad) Supplies Additional Authenticated Data (GCM).withDecryptionIv(byte[] iv) Supplies an out-of-band IV for decryption without an encoded header.Convenience forwithHeaderCodec(SymmetricHeaderCodec)usingAesHeaderCodec.Installs a specific header codec into the spec so IV/tagBits/AAD hash can be persisted in‑band.Sets the key to use.
-
Method Details
-
builder
Creates a new AES builder with default spec (GCM/128, no header) and no key selected. UsewithKey(SecretKey)(recommended), or import/generate a key, then select a mode.- Parameters:
session- active session used to create cryptographic contexts- Returns:
- a new builder instance
-
withKey
Sets the key to use. Overrides any previous generate/import choice.- Parameters:
k- the AESSecretKey; must be 128/192/256-bit- Returns:
- this builder
- Throws:
NullPointerException- ifkis null
-
generateKey
Requests generation of a fresh AES key of the given size.Note: prefer
withKey(SecretKey)for pipelines that need predictable key reuse across encrypt/decrypt runs.- Parameters:
bits- 128, 192, or 256- Returns:
- this builder
- Throws:
IllegalArgumentException- if size is invalid
-
importKeyRaw
Imports a raw AES key (16/24/32 bytes).- Parameters:
raw- key material; copied defensively- Returns:
- this builder
- Throws:
IllegalArgumentException- if length is not 16/24/32
-
importKeyHex
Imports a hex‑encoded AES key (32/48/64 hex chars).- Parameters:
hex- hex string; not null- Returns:
- this builder
- Throws:
NullPointerException- ifhexis nullIllegalArgumentException- if the decoded length is not 16/24/32
-
importKeyBase64
Imports a Base64‑encoded AES key (no padding required).- Parameters:
b64- Base64 text; not null- Returns:
- this builder
- Throws:
NullPointerException- ifb64is nullIllegalArgumentException- if the decoded length is not 16/24/32
-
spec
Copies mode/padding/tagBits/header from a fully builtAesSpec.- Parameters:
s- the spec to apply- Returns:
- this builder
- Throws:
NullPointerException- ifsis null
-
modeGcm
Selects GCM and sets the authentication tag length in bits.- Parameters:
tagBits- 96..128 (step 8)- Returns:
- this builder
- Throws:
IllegalArgumentException- iftagBitsis out of range
-
modeCtr
Selects CTR with NOPADDING. Pair with a MAC if integrity is required.- Returns:
- this builder
-
modeCbcPkcs5
-
modeCbcNoPadding
Selects CBC/NOPADDING. The plaintext length must be a multiple of the block size.- Returns:
- this builder
-
withDecryptionIv
Supplies an out-of-band IV for decryption without an encoded header.- Parameters:
iv- the IV (12 bytes for GCM, 16 bytes for CBC/CTR)- Returns:
- this builder
- Throws:
NullPointerException- ifivisnull
-
withAad
Supplies Additional Authenticated Data (GCM). AAD is not encrypted; it is bound to the ciphertext. If a header is used, only a SHA‑256 hash of AAD is written.- Parameters:
aad- AAD bytes (may be empty)- Returns:
- this builder
-
withHeaderCodec
Installs a specific header codec into the spec so IV/tagBits/AAD hash can be persisted in‑band.- Parameters:
codec- the codec to use (e.g.,AesHeaderCodec)- Returns:
- this builder
-
withHeader
Convenience forwithHeaderCodec(SymmetricHeaderCodec)usingAesHeaderCodec.- Returns:
- this builder
-
context
Provides a Conflux session context for exchanging IV/AAD/tagBits and enabling header I/O. When null, no context features are used and no header is written/read.- Parameters:
ctx- the context, ornull- Returns:
- this builder
-
build
Builds an encrypting or decryptingDataContent.- Resolves the key from
withKey(SecretKey)or the selected generate/import spec. - Finalizes
AesSpec(injecting the header if configured). - Propagates a decryption IV and AAD into the context when supplied.
- Specified by:
buildin interfaceDataContentBuilder<DataContent>- Parameters:
encrypt-truefor encryption;falsefor decryption- Returns:
- a
DataContentthat transforms its upstream stream - Throws:
IllegalStateException- on key construction errors or provider misconfiguration
- Resolves the key from
-
generatedKey
Returns the generated secret key associated with this instance.The value is typically produced during a key generation process and stored internally for later retrieval. The returned reference is the actual
SecretKeyobject; callers should avoid modifying or exposing it unnecessarily to preserve security.- Returns:
- the generated
SecretKey, ornullif no key has been generated
-