processBytes method

  1. @override
void processBytes(
  1. Uint8List inp,
  2. int inpOff,
  3. int len,
  4. Uint8List out,
  5. int outOff,
)
override

Process len bytes of data given by inp and starting at offset inpOff. The resulting cipher text is put in out beginning at position outOff.

Implementation

@override
void processBytes(
    Uint8List inp, int inpOff, int len, Uint8List out, int outOff) {
  if (!_initialised) {
    throw StateError('ChaCha20 not initialized: please call init() first');
  }

  if ((inpOff + len) > inp.length) {
    throw ArgumentError(
        'Input buffer too short or requested length too long');
  }

  if ((outOff + len) > out.length) {
    throw ArgumentError(
        'Output buffer too short or requested length too long');
  }

  for (var i = 0; i < len; i++) {
    if (_keyStreamOffset == 0) {
      generateKeyStream(_keyStream);

      if (++_state[12] == 0) throw StateError('Illegal increase of counter');
    }

    out[i + outOff] = clip8(_keyStream[_keyStreamOffset] ^ inp[i + inpOff]);
    _keyStreamOffset = (_keyStreamOffset + 1) & 63;
  }
}