Class JcaDigestContext
- All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, DigestContext, TagEngine<byte[]>
MessageDigest to a
pull-based streaming pipeline.
JcaDigestContext implements DigestContext for
Sha2Sha3Algorithm by delegating to a JCA
MessageDigest and exposing the TagEngine
contract. As data is read from the wrapped stream the digest state is
updated; at end-of-stream the digest is finalized and either appended as a
trailer (produce mode) or compared against an expected tag (verify mode).
Features
- Supports SHA-2, SHA-3, and SHAKE digests as defined by
DigestSpec. - Provides pull-stream semantics via
wrap(java.io.InputStream). - Two modes:
- Produce mode: appends the computed digest as a trailer.
- Verify mode: computes the digest and compares it to an expected
tag using a pluggable verification approach (see
setVerificationApproach(ThrowingBiPredicate.VerificationBiPredicate)).
- For XOFs (SHAKE), the output length is explicitly controlled by
DigestSpec.
Verification approach
Verification is delegated to a
ThrowingBiPredicate.VerificationBiPredicate over byte[]. The
default core returned by getVerificationCore() performs
constant-time byte comparison (ByteVerificationStrategy). If no
strategy is set explicitly, the effective verifier defaults to
getVerificationCore().getThrowOnMismatch(), causing a mismatch to be
signaled as an I/O error at end-of-stream.
Thread-safety
Instances are stateful and not thread-safe. Each context may wrap exactly one
stream; subsequent calls to wrap(java.io.InputStream) are rejected.
- Since:
- 1.0
-
Constructor Summary
ConstructorsConstructorDescriptionJcaDigestContext(CryptoAlgorithm algorithm, MessageDigest md, DigestSpec spec) Creates a new digest context bound to a JCAMessageDigest. -
Method Summary
Modifier and TypeMethodDescriptionReturns theCryptoAlgorithmdefinition that created this context.voidclose()Closes the active digest stream if one was opened.Returns the core verification predicate for digest tags.key()Returns the key associated with this context.voidsetExpectedTag(byte[] expected) Installs an expected digest tag for verification mode.voidsetVerificationApproach(ThrowingBiPredicate.VerificationBiPredicate<byte[]> strategy) Sets the verification approach used to compare computed and expected digests.intReturns the digest length in bytes.wrap(InputStream upstream) Wraps an upstreamInputStreamin a digesting pipeline stage.
-
Constructor Details
-
JcaDigestContext
Creates a new digest context bound to a JCAMessageDigest.This constructor is normally invoked by
Sha2Sha3Algorithm:MessageDigest md = MessageDigest.getInstance(spec.algorithm().jca()); return new JcaDigestContext(algo, md, spec);- Parameters:
algorithm- the owning algorithm descriptormd- JCA message digest implementationspec- digest parameters (algorithm, output length for XOFs)- Throws:
NullPointerException- if any parameter is nullIllegalArgumentException- if the output length for an XOF is invalid
-
-
Method Details
-
algorithm
Returns theCryptoAlgorithmdefinition that created this context.- Specified by:
algorithmin interfaceCryptoContext- Returns:
- algorithm descriptor
-
key
Returns the key associated with this context.Digests are unkeyed; this method always returns
null.- Specified by:
keyin interfaceCryptoContext- Returns:
- always
null
-
close
public void close()Closes the active digest stream if one was opened.The close operation is non-throwing; any underlying
IOExceptionis suppressed to preserve pipeline robustness.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceCryptoContext
-
wrap
Wraps an upstreamInputStreamin a digesting pipeline stage.Data read from the returned stream is passed through unchanged while being accumulated into the underlying
MessageDigest. At end-of-stream the digest is finalized and either emitted as a trailer (produce mode) or compared to the expected tag (verify mode).This method may be called at most once per context instance.
- Specified by:
wrapin interfaceTagEngine<byte[]>- Parameters:
upstream- the input stream to wrap; must not benull- Returns:
- a stream that transparently digests data
- Throws:
IOException- if stream initialization failsIllegalStateException- ifwrapwas already invoked on this instanceNullPointerException- ifupstreamisnull
-
tagLength
-
setExpectedTag
public void setExpectedTag(byte[] expected) Installs an expected digest tag for verification mode.When provided, the context operates in verification mode: at end-of-stream the computed digest is compared against this tag using the effective verification strategy. Passing
nullclears the tag and leaves the mode unchanged.- Specified by:
setExpectedTagin interfaceTagEngine<byte[]>- Parameters:
expected- expected digest bytes; copied defensively (may benullto clear)
-
setVerificationApproach
Sets the verification approach used to compare computed and expected digests.If no strategy is set explicitly, the effective verifier defaults to
getVerificationCore().getThrowOnMismatch().- Specified by:
setVerificationApproachin interfaceTagEngine<byte[]>- Parameters:
strategy- verification predicate; may benullto keep the default behavior
-
getVerificationCore
Returns the core verification predicate for digest tags.The default core is a constant-time byte-array comparison implemented by
ByteVerificationStrategy.- Specified by:
getVerificationCorein interfaceTagEngine<byte[]>- Returns:
- base verification predicate (never
null)
-