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 creates PlainContent 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 via context(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 Details

    • builder

      public static DigestDataContentBuilder builder(ZeroEchoSession session)
      Creates a new builder for constructing digest-computing PlainContent pipelines.

      Example

      PlainContent pc = DigestDataContentBuilder.builder()
          .sha256()
          .emitRawDigest()
          .build(true);
      
      Parameters:
      session - active session used to create cryptographic contexts
      Returns:
      a new DigestDataContentBuilder instance
    • sha256

      public DigestDataContentBuilder sha256()
      Selects SHA-256 as the digest algorithm.
      Returns:
      this builder for chaining
    • sha384

      public DigestDataContentBuilder sha384()
      Selects SHA-384 as the digest algorithm.
      Returns:
      this builder for chaining
    • sha512

      public DigestDataContentBuilder sha512()
      Selects SHA-512 as the digest algorithm.
      Returns:
      this builder for chaining
    • sha3_256

      public DigestDataContentBuilder sha3_256()
      Selects SHA3-256 as the digest algorithm.
      Returns:
      this builder for chaining
    • sha3_512

      public DigestDataContentBuilder sha3_512()
      Selects SHA3-512 as the digest algorithm.
      Returns:
      this builder for chaining
    • shake128

      public DigestDataContentBuilder shake128(int outLenBytes)
      Selects SHAKE128 with a given output length.
      Parameters:
      outLenBytes - the desired digest length in bytes
      Returns:
      this builder for chaining
      Throws:
      IllegalArgumentException - if outLenBytes is invalid for the implementation
    • shake256

      public DigestDataContentBuilder shake256(int outLenBytes)
      Selects SHAKE256 with a given output length.
      Parameters:
      outLenBytes - the desired digest length in bytes
      Returns:
      this builder for chaining
      Throws:
      IllegalArgumentException - if outLenBytes is invalid for the implementation
    • passThrough

      public DigestDataContentBuilder passThrough()
      Configures the builder to pass the input through unchanged and strip the trailing tag.
      Returns:
      this builder for chaining
    • emitRawDigest

      public DigestDataContentBuilder emitRawDigest()
      Configures the builder to emit the finalized digest as raw bytes.
      Returns:
      this builder for chaining
    • emitHexDigest

      public DigestDataContentBuilder emitHexDigest()
      Configures the builder to emit the finalized digest as lowercase hexadecimal text.
      Returns:
      this builder for chaining
    • emitBase64Digest

      public DigestDataContentBuilder emitBase64Digest()
      Configures the builder to emit the finalized digest as Base64 text without padding.
      Returns:
      this builder for chaining
    • bufferSize

      public DigestDataContentBuilder bufferSize(int bytes)
      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:
      this builder for chaining
      Throws:
      IllegalArgumentException - if bytes < 1
    • context

      public DigestDataContentBuilder context(CtxInterface ctx)
      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:
      this builder for chaining
    • storeInCtx

      public DigestDataContentBuilder storeInCtx(Key<byte[]> key)
      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:
      this builder for chaining
    • onDigest

      public DigestDataContentBuilder onDigest(Consumer<byte[]> cb)
      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:
      this builder for chaining
    • build

      public PlainContent build(boolean encryptFlagIgnored)
      Builds a digest-computing PlainContent according to the current configuration.

      The boolean parameter is ignored and present only to satisfy the DataContentBuilder interface.

      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:
      build in interface DataContentBuilder<PlainContent>
      Parameters:
      encryptFlagIgnored - ignored parameter from the interface
      Returns:
      a PlainContent pipeline that performs the selected digest behavior when its stream is consumed