Interface MacContext

All Superinterfaces:
AutoCloseable, Closeable, CryptoContext, TagEngine<byte[]>
All Known Implementing Classes:
HmacMacContext

public non-sealed interface MacContext extends TagEngine<byte[]>, CryptoContext
Context for computing message authentication codes (MACs) in streaming pipelines.

A MacContext encapsulates the state of a keyed integrity primitive such as HMAC, KMAC, or CMAC and exposes the TagEngine contract: the wrapped input stream passes bytes through while the MAC state is updated; at end-of-file a fixed-length tag is either appended (produce mode) or compared with an expected tag (verify mode).

Operation

Usage

Produce an HMAC trailer


javax.crypto.SecretKey key = ...;
HmacSpec spec = HmacSpec.sha256();

TagEngine<byte[]> eng = TagEngineBuilder.hmac(key, spec).get();
try (java.io.InputStream in = eng.wrap(upstream)) {
    in.transferTo(out); // body bytes, then HMAC trailer are written to 'out'
}

Verify a detached MAC


byte[] expectedMac = ...; // obtained via a trusted channel

TagEngine<byte[]> eng = TagEngineBuilder.hmac(key, HmacSpec.sha256()).get();
// Optional: throw on mismatch instead of silent flagging
eng.setVerificationApproach(eng.getVerificationCore().getThrowOnMismatch());
eng.setExpectedTag(expectedMac);

try (java.io.InputStream in = eng.wrap(bodyWithoutTrailer)) {
    in.transferTo(java.io.OutputStream.nullOutputStream()); // comparison at EOF
}

Security considerations

Since:
1.0