setBlock method

  1. @override
void setBlock(
  1. int blockIndex
)
override

Fills block with a new key stream block.

Implementation

@override
void setBlock(int blockIndex) {
  // Fill state with zeroes
  final stateAsUint32List = blockAsUint32List;
  stateAsUint32List.fillRange(0, 16, 0);

  // Set block counter
  final initialState = _initialState;

  // In AEAD_CHACHA20_POLY1305, the counter is set to 1 for the first block
  // because the first block is used for the MAC.
  // Thus its `cipher.macAlgorithm.keyStreamUsed` will return 64.
  initialState[12] = cipher.macAlgorithm.keyStreamUsed ~/ 64 + blockIndex;

  // Compute key stream block
  DartChacha20.chachaRounds(
    stateAsUint32List,
    0,
    initialState,
    rounds: 20,
  );
}