Class HmacDataContentBuilder
- All Implemented Interfaces:
DataContentBuilder<PlainContent>
Overview
The builder producesPlainContent that either passes the original
bytes through while computing or verifying an HMAC trailer, or emits only the
tag or a boolean verification result. The HMAC variant is selected via
spec(HmacSpec) or convenience methods such as sha256().
Modes and outputs
- MAC mode: compute an HMAC over the incoming stream and either pass the body through with the trailer stripped or emit the finalized tag in raw, hex, or Base64 form.
- VERIFY mode: verify an expected HMAC. The expected tag can be provided directly or taken from the input trailer. The output can be pass-through of the body or a textual boolean.
Key handling
Keys may be supplied directly, generated for the configured MAC name, or imported from raw, hex, or Base64 text. If no key material is provided, a default key suitable for the selected MAC is generated.Thread-safety
Instances are mutable and not thread-safe. Configure and use each builder instance from a single thread.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumMode selects whether the pipeline computes an HMAC tag or verifies one. -
Method Summary
Modifier and TypeMethodDescriptionbuild(boolean ignoredEncryptFlag) Builds a digesting or verifyingPlainContentpipeline according to the current configuration.static HmacDataContentBuilderbuilder(ZeroEchoSession session) Creates a new builder for constructing HMAC computing or verifying pipelines.Configures the pipeline to emit the finalized HMAC tag as Base64 text.Configures the pipeline to emit the finalized HMAC tag as lowercase hexadecimal text.Configures the pipeline to emit the finalized HMAC tag as raw bytes.Configures the pipeline to verify and emit a textual boolean result.expectedTag(byte[] raw) Supplies the expected HMAC tag as raw bytes for verification.expectedTagBase64(String b64) Supplies the expected HMAC tag as a Base64 string for verification.expectedTagHex(String hex) Supplies the expected HMAC tag as a hexadecimal string for verification.generateKey(int bits) Requests generation of a new secret key sized for the configured MAC.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 bytes.mac()Switches the builder to MAC mode.Configures the pipeline to pass the input body through unchanged and strip the engine's trailer.intReturns a recommended HMAC key size (in bits) for the currently configuredspec().sha256()Selects HmacSHA256.sha384()Selects HmacSHA384.sha512()Selects HmacSHA512.spec()Returns the currently configured HMAC specification.Sets the HMAC specification to use for this pipeline.verify()Switches the builder to VERIFY mode.Supplies an explicit secret key for the HMAC operation.
-
Method Details
-
builder
Creates a new builder for constructing HMAC computing or verifying pipelines.Example
PlainContent pc = HmacDataContentBuilder.builder() .sha256() .emitHexTag() .build(true);- Parameters:
session- active session used to create cryptographic contexts- Returns:
- a new
HmacDataContentBuilderinstance
-
spec
Returns the currently configured HMAC specification.This accessor is intentionally read-only and exists to support safe integrations (for example hybrid-derived key injection) without duplicating the HMAC variant configuration outside of this builder.
- Returns:
- current HMAC spec (never null)
- Since:
- 1.0
-
recommendedKeyBits
public int recommendedKeyBits()Returns a recommended HMAC key size (in bits) for the currently configuredspec().This is a convenience forwarding method to
HmacSpec.recommendedKeyBits()and is intended as the default choice for derived-key integrations. Callers that intentionally need a non-default size may override it explicitly.- Returns:
- recommended key size in bits (positive, multiple of 8)
- Since:
- 1.0
-
mac
Switches the builder to MAC mode.- Returns:
thisbuilder for chaining
-
verify
Switches the builder to VERIFY mode.- Returns:
thisbuilder for chaining
-
spec
Sets the HMAC specification to use for this pipeline.- Parameters:
s- the HMAC spec; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifsis null
-
sha256
-
sha384
-
sha512
-
withKey
Supplies an explicit secret key for the HMAC operation.Calling this clears any prior key generation or import configuration.
- Parameters:
k- the secret key; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifkis null
-
generateKey
Requests generation of a new secret key sized for the configured MAC.The key is generated for the MAC name returned by
HmacSpec.macName().- Parameters:
bits- desired key length in bits- Returns:
thisbuilder for chaining
-
importKeyRaw
Imports a secret key from raw bytes.Calling this clears any prior explicit key or generation request.
- Parameters:
raw- the raw key bytes; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifrawis null
-
importKeyHex
Imports a secret key from a hexadecimal string.Calling this clears any prior explicit key or generation request.
- Parameters:
hex- hex-encoded key text; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifhexis nullIllegalArgumentException- ifhexis not valid hexadecimal
-
importKeyBase64
Imports a secret key from a Base64 string.Calling this clears any prior explicit key or generation request.
- Parameters:
b64- Base64-encoded key text; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifb64is nullIllegalArgumentException- ifb64is not valid Base64
-
passThrough
Configures the pipeline to pass the input body through unchanged and strip the engine's trailer.- Returns:
thisbuilder for chaining
-
emitRawTag
Configures the pipeline to emit the finalized HMAC tag as raw bytes.Switches the mode to
HmacDataContentBuilder.Mode.MAC.- Returns:
thisbuilder for chaining
-
emitHexTag
Configures the pipeline to emit the finalized HMAC tag as lowercase hexadecimal text.Switches the mode to
HmacDataContentBuilder.Mode.MAC.- Returns:
thisbuilder for chaining
-
emitBase64Tag
Configures the pipeline to emit the finalized HMAC tag as Base64 text.Switches the mode to
HmacDataContentBuilder.Mode.MAC.- Returns:
thisbuilder for chaining
-
emitVerificationBoolean
Configures the pipeline to verify and emit a textual boolean result.Switches the mode to
HmacDataContentBuilder.Mode.VERIFY.- Returns:
thisbuilder for chaining
-
expectedTag
Supplies the expected HMAC tag as raw bytes for verification.Providing a value here clears any prior hex or Base64 expected tag.
- Parameters:
raw- the expected tag bytes; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifrawis null
-
expectedTagHex
Supplies the expected HMAC tag as a hexadecimal string for verification.Providing a value here clears any prior raw or Base64 expected tag.
- Parameters:
hex- the expected tag in hexadecimal; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifhexis nullIllegalArgumentException- ifhexis not valid hexadecimal
-
expectedTagBase64
Supplies the expected HMAC tag as a Base64 string for verification.Providing a value here clears any prior raw or hex expected tag.
- Parameters:
b64- the expected tag in Base64; must not be null- Returns:
thisbuilder for chaining- Throws:
NullPointerException- ifb64is nullIllegalArgumentException- ifb64is not valid Base64
-
build
Builds a digesting or verifyingPlainContentpipeline according to the current configuration.The boolean parameter is ignored and exists only to satisfy the
DataContentBuilderinterface. The actual behavior is controlled bymac()orverify()and the selected output.Result
- MAC mode: returns a pass-through content or a tag-emitting content based
on
passThrough(),emitRawTag(),emitHexTag(), oremitBase64Tag(). - VERIFY mode: returns a pass-through content that throws on mismatch or an
emitter of "true"/"false" based on
emitVerificationBoolean().
- Specified by:
buildin interfaceDataContentBuilder<PlainContent>- Parameters:
ignoredEncryptFlag- unused parameter from the interface- Returns:
- a
PlainContentpipeline performing the configured HMAC operation - Throws:
IllegalStateException- if an invalid mode/output combination is selected or key construction fails
- MAC mode: returns a pass-through content or a tag-emitting content based
on
-