Class DigestDataContentBuilder
java.lang.Object
zeroecho.sdk.builders.alg.DigestDataContentBuilder
- All Implemented Interfaces:
DataContentBuilder<PlainContent>
public final class DigestDataContentBuilder
extends Object
implements DataContentBuilder<PlainContent>
DigestDataContentBuilder constructs streaming pipelines that compute message
digests while reading an InputStream.
Overview
The builder createsPlainContent that either passes
the original bytes through and strips the trailing digest tag from the
underlying engine, or emits the finalized digest itself in raw, hexadecimal,
or Base64 form. Algorithm selection is performed via convenience methods such
as sha256() or shake256(int) which configure an internal
DigestSpec.
Output modes
- Pass-through: the output stream is identical to the input stream while the digest is computed under the hood. The trailing tag produced by the engine is stripped off transparently.
- Emit digest: the output stream contains only the digest, encoded as raw bytes, hex, or Base64.
Context integration
A runtime context can be supplied viacontext(conflux.CtxInterface). When provided together with
storeInCtx(conflux.Key), the finalized digest bytes are stored in
the context. A callback may also be registered via
onDigest(java.util.function.Consumer) to receive a defensive copy of
the digest.
Examples
// 1) Pass-through SHA-256 that stores the digest in a context:
PlainContent content = DigestDataContentBuilder.builder()
.sha256()
.context(ctx)
.storeInCtx(digestKey)
.passThrough()
.build(true);
// 2) Emit hex-encoded SHA3-512 digest:
PlainContent hexOut = DigestDataContentBuilder.builder()
.sha3_512()
.emitHexDigest()
.build(true);
// 3) Emit 32-byte SHAKE128 digest in Base64 and also receive it via a callback:
PlainContent b64Out = DigestDataContentBuilder.builder()
.shake128(32)
.onDigest(bytes -> System.out.println("len=" + bytes.length))
.emitBase64Digest()
.build(true);
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 TypeMethodDescriptionbufferSize(int bytes) Sets the internal buffer size used by the tail-stripping wrapper.build(boolean encryptFlagIgnored) Builds a digest-computingPlainContentaccording to the current configuration.static DigestDataContentBuilderbuilder(ZeroEchoSession session) Creates a new builder for constructing digest-computingPlainContentpipelines.context(CtxInterface ctx) Sets an optional runtime context used to store the finalized digest when requested.Configures the builder to emit the finalized digest as Base64 text without padding.Configures the builder to emit the finalized digest as lowercase hexadecimal text.Configures the builder to emit the finalized digest as raw bytes.Registers a callback to receive a defensive copy of the finalized digest.Configures the builder to pass the input through unchanged and strip the trailing tag.sha256()Selects SHA-256 as the digest algorithm.sha3_256()Selects SHA3-256 as the digest algorithm.sha3_512()Selects SHA3-512 as the digest algorithm.sha384()Selects SHA-384 as the digest algorithm.sha512()Selects SHA-512 as the digest algorithm.shake128(int outLenBytes) Selects SHAKE128 with a given output length.shake256(int outLenBytes) Selects SHAKE256 with a given output length.storeInCtx(Key<byte[]> key) Requests that the finalized digest be written into the context under the provided key.
-
Method Details
-
builder
Creates a new builder for constructing digest-computingPlainContentpipelines.Example
PlainContent pc = DigestDataContentBuilder.builder() .sha256() .emitRawDigest() .build(true);- Parameters:
session- active session used to create cryptographic contexts- Returns:
- a new
DigestDataContentBuilderinstance
-
sha256
Selects SHA-256 as the digest algorithm.- Returns:
thisbuilder for chaining
-
sha384
Selects SHA-384 as the digest algorithm.- Returns:
thisbuilder for chaining
-
sha512
Selects SHA-512 as the digest algorithm.- Returns:
thisbuilder for chaining
-
sha3_256
Selects SHA3-256 as the digest algorithm.- Returns:
thisbuilder for chaining
-
sha3_512
Selects SHA3-512 as the digest algorithm.- Returns:
thisbuilder for chaining
-
shake128
Selects SHAKE128 with a given output length.- Parameters:
outLenBytes- the desired digest length in bytes- Returns:
thisbuilder for chaining- Throws:
IllegalArgumentException- ifoutLenBytesis invalid for the implementation
-
shake256
Selects SHAKE256 with a given output length.- Parameters:
outLenBytes- the desired digest length in bytes- Returns:
thisbuilder for chaining- Throws:
IllegalArgumentException- ifoutLenBytesis invalid for the implementation
-
passThrough
Configures the builder to pass the input through unchanged and strip the trailing tag.- Returns:
thisbuilder for chaining
-
emitRawDigest
Configures the builder to emit the finalized digest as raw bytes.- Returns:
thisbuilder for chaining
-
emitHexDigest
Configures the builder to emit the finalized digest as lowercase hexadecimal text.- Returns:
thisbuilder for chaining
-
emitBase64Digest
Configures the builder to emit the finalized digest as Base64 text without padding.- Returns:
thisbuilder for chaining
-
bufferSize
Sets the internal buffer size used by the tail-stripping wrapper.- Parameters:
bytes- buffer size in bytes; must be greater than or equal to 1- Returns:
thisbuilder for chaining- Throws:
IllegalArgumentException- ifbytes < 1
-
context
Sets an optional runtime context used to store the finalized digest when requested.- Parameters:
ctx- the context instance; may be null to disable context storage- Returns:
thisbuilder for chaining
-
storeInCtx
Requests that the finalized digest be written into the context under the provided key.This setting is only effective if a non-null context is provided via
context(CtxInterface).- Parameters:
key- the context key used to store the digest; may be null to clear- Returns:
thisbuilder for chaining
-
onDigest
Registers a callback to receive a defensive copy of the finalized digest.- Parameters:
cb- the callback to invoke after the digest is computed; may be null to clear- Returns:
thisbuilder for chaining
-
build
Builds a digest-computingPlainContentaccording to the current configuration.The boolean parameter is ignored and present only to satisfy the
DataContentBuilderinterface.Result
- Pass-through mode returns content that outputs the original bytes while computing the digest and stripping the trailing tag.
- Emit modes return content whose output is only the digest in the requested encoding.
- Specified by:
buildin interfaceDataContentBuilder<PlainContent>- Parameters:
encryptFlagIgnored- ignored parameter from the interface- Returns:
- a
PlainContentpipeline that performs the selected digest behavior when its stream is consumed
-