Class HmacMacContext

java.lang.Object
zeroecho.core.alg.hmac.HmacMacContext
All Implemented Interfaces:
Closeable, AutoCloseable, CryptoContext, MacContext, TagEngine<byte[]>

public final class HmacMacContext extends Object implements MacContext
Streaming HMAC context backed by the JCA Mac API.

HmacMacContext implements MacContext and integrates with the TagEngine contract to support both production and verification in a pull-based streaming pipeline. One instance is intended for one wrapped stream.

TagEngine contract

CryptoContext contract

Verification approach

The core comparison strategy is provided by getVerificationCore(), which by default returns a constant-time byte-array comparator (ByteVerificationStrategy). If no explicit strategy is set, wrap(InputStream) applies that core strategy decorated with getThrowOnMismatch() so that mismatches raise an exception at EOF.

Thread-safety

This context is stateful and not thread-safe. After wrap(InputStream) has been called once, further calls are rejected.

Usage example

Produce HMAC-SHA256 trailer


SecretKey key = session.keyBuilders().symmetric().generate("HMAC", HmacKeyGenSpec.sha256(256));
HmacMacContext ctx = new HmacMacContext(CryptoAlgorithms.require("HMAC"), key, "HmacSHA256");
try (InputStream in = ctx.wrap(new FileInputStream("data.bin"))) {
    in.transferTo(OutputStream.nullOutputStream()); // body then MAC trailer
}

Verify with explicit strategy


HmacMacContext ctx = new HmacMacContext(CryptoAlgorithms.require("HMAC"), key, "HmacSHA256");
ctx.setVerificationApproach(ctx.getVerificationCore().getThrowOnMismatch());
ctx.setExpectedTag(expectedTag);
try (InputStream in = ctx.wrap(new FileInputStream("body.bin"))) {
    in.transferTo(OutputStream.nullOutputStream()); // throws on mismatch at EOF
}

Since:
1.0