Class ChaChaHeaderCodec
java.lang.Object
zeroecho.core.alg.chacha.ChaChaHeaderCodec
- All Implemented Interfaces:
SymmetricHeaderCodec
ChaCha20 streaming header codec
ImplementsSymmetricHeaderCodec for the ChaCha20 stream
cipher. Encodes a compact header containing:
- a 12-byte nonce (IV), and
- the stream counter as a 7-bit packed integer.
The nonce and counter are exchanged through the bound CtxInterface
using ConfluxKeys.iv(String) and ConfluxKeys.tagBits(String)
keys, namespaced by the algorithm id (e.g., "CHACHA20").
Header layout
[0..11] : 12-byte nonce (IV) [12..N] : counter encoded via 7-bit packed integer
Behavior
- writeHeader: requires
iv(id)in context (12 bytes). Writes the nonce, then the counter fromtagBits(id)if present, otherwise uses1. - readHeader: reads nonce and counter and stores them into the context under the same keys.
- Since:
- 1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionreadHeader(InputStream in, CryptoAlgorithm algorithm, CtxInterface ctx) Reads a ChaCha20 header from the input and hydrates the context.voidwriteHeader(OutputStream out, CryptoAlgorithm algorithm, CtxInterface ctx) Writes the ChaCha20 header to the provided output.
-
Constructor Details
-
ChaChaHeaderCodec
public ChaChaHeaderCodec()Creates a stateless ChaCha20 stream-header codec.
-
-
Method Details
-
writeHeader
public void writeHeader(OutputStream out, CryptoAlgorithm algorithm, CtxInterface ctx) throws IOException Writes the ChaCha20 header to the provided output.Reads a 12-byte nonce from
ConfluxKeys.iv(String)and a stream counter fromConfluxKeys.tagBits(String)(defaulting to1if absent). Emits the nonce followed by the counter encoded as a 7-bit packed integer, then flushes.- Specified by:
writeHeaderin interfaceSymmetricHeaderCodec- Parameters:
out- destination streamalgorithm- algorithm instance used for context key scopingctx- operation context carrying nonce and optional counter- Throws:
IOException- if the nonce is missing/invalid or I/O fails
-
readHeader
public InputStream readHeader(InputStream in, CryptoAlgorithm algorithm, CtxInterface ctx) throws IOException Reads a ChaCha20 header from the input and hydrates the context.Consumes a 12-byte nonce and a 7-bit packed counter, then stores them into
ctxunderConfluxKeys.iv(String)andConfluxKeys.tagBits(String), respectively. Returns the same input stream positioned after the header.- Specified by:
readHeaderin interfaceSymmetricHeaderCodec- Parameters:
in- source streamalgorithm- algorithm instance used for context key scopingctx- context to populate with nonce and counter- Returns:
- the input stream positioned after the header
- Throws:
IOException- if the header is malformed or I/O fails
-