addSlice method

  1. @mustCallSuper
  2. @override
void addSlice(
  1. List<int> chunk,
  2. int start,
  3. int end,
  4. bool isLast,
)
override

Adds the next chunk to this.

Adds the bytes defined by start and end-exclusive to this.

If isLast is true closes this.

Contrary to add the given chunk must not be held onto. Once the method returns, it is safe to overwrite the data in it.

Implementation

@mustCallSuper
@override
void addSlice(List<int> chunk, int start, int end, bool isLast) {
  if (isClosed) {
    throw StateError('Closed already');
  }
  RangeError.checkValidRange(start, end, chunk.length);

  // We need to support browsers, which don't have 64-bit integers.
  //
  // We originally had a BigInt implementation, but it was slow.
  // This new implementation uses 16-bit integer arrays.
  //
  // The implementation has been inspired by "poly1305-donna-16.h" by Andrew
  // Moon, which has "MIT or PUBLIC DOMAIN" license. It can be found at:
  // https://github.com/floodyberry/poly1305-donna/blob/master/poly1305-donna-16.h
  final chunkLength = end - start;
  if (chunkLength > 0) {
    // Increment length
    _length += chunkLength;

    // For each byte
    final buffer = _buffer;
    var blockLength = _blockLength;
    for (var i = start; i < end; i++) {
      // Set byte
      buffer.setUint8(blockLength, chunk[i]);
      blockLength++;

      // Full block?
      if (blockLength == 16) {
        buffer.setUint8(16, 1);
        process(
          block: buffer,
          blockLength: 16,
          a: _a,
          r: _r,
          s: _s,
          tmp: _tmp,
          isLast: false,
        );
        blockLength = 0;
      }
    }
    _blockLength = blockLength;
  }

  if (isLast) {
    // Call a protected method we needed for implementing
    // ChaCha20-Poly1305-AEAD
    afterData();

    // Final block
    _finalize();
  }
}