convertChunkSync method

  1. @nonVirtual
  2. @override
List<int> convertChunkSync(
  1. List<int> input, {
  2. Uint8List? possibleBuffer,
})
override

Adds input to the sequence of converted bytes. Returns a list of converted bytes, which may be empty or larger the input.

You MUST later call convert, which will finish converting.

Implementation

@nonVirtual
@override
List<int> convertChunkSync(
  List<int> input, {
  Uint8List? possibleBuffer,
}) {
  if (!_isInitialized) {
    throw StateError('Not initialized');
  }
  if (input.isEmpty) {
    return input;
  }
  final macSink = this.macSink;
  final block = this.block;
  final blockLength = block.length;
  var allowUseSameBytes = identical(input, possibleBuffer);
  if (!allowUseSameBytes) {
    input = Uint8List.fromList(input);
  }

  // If we are decrypting, the input is now the cipher text.
  // So we add it to the sink.
  if (!_isEncrypting) {
    macSink.add(input);
  }

  var keyStreamIndex = this.keyStreamIndex;

  // Compute block if needed
  if (keyStreamIndex % blockLength != 0) {
    final blockIndex = keyStreamIndex ~/ blockLength;
    if (blockIndex != _blockIndex) {
      setBlock(blockIndex);
      _blockIndex = blockIndex;
    }
  }

  for (var i = 0; i < input.length; i++) {
    // First byte of a block?
    final indexInBlock = keyStreamIndex % blockLength;
    if (indexInBlock == 0) {
      final blockIndex = keyStreamIndex ~/ blockLength;
      setBlock(blockIndex);
      _blockIndex = blockIndex;
    }

    // XOR
    input[i] ^= block[indexInBlock];
    keyStreamIndex++;
  }
  this.keyStreamIndex = keyStreamIndex;

  // If we are encrypting, the input is now the cipher text.
  // So we add it to the sink.
  // (If we are decrypting, we added it earlier)
  if (_isEncrypting) {
    macSink.add(input);
  }
  return input;
}