processBytes method

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

Implementation

@override
int processBytes(
    Uint8List inp, int inOff, int len, Uint8List out, int outOff) {
  if (inOff < 0) {
    throw ArgumentError('\'inOff\' cannot be negative');
  }
  if (len < 0) {
    throw ArgumentError('\'len\' cannot be negative');
  }
  if (inOff > (inp.length - len)) {
    throw ArgumentError('Input buffer too short');
  }
  if (outOff < 0) {
    throw ArgumentError('\'outOff\' cannot be negative');
  }

  checkData();

  var resultLen = 0;

  switch (_state) {
    case State.DEC_DATA:
      {
        for (var i = 0; i < len; ++i) {
          _buf[_bufPos] = inp[inOff + i];
          if (++_bufPos == _buf.length) {
            poly1305.update(_buf, 0, BUF_SIZE);
            processData(_buf, 0, BUF_SIZE, out, outOff + resultLen);
            utils.arrayCopy(_buf, BUF_SIZE, _buf, 0, MAC_SIZE);
            _bufPos = MAC_SIZE;
            resultLen += BUF_SIZE;
          }
        }
        break;
      }
    case State.ENC_DATA:
      {
        if (_bufPos != 0) {
          while (len > 0) {
            --len;
            _buf[_bufPos] = inp[inOff++];
            if (++_bufPos == BUF_SIZE) {
              processData(_buf, 0, BUF_SIZE, out, outOff);
              poly1305.update(out, outOff, BUF_SIZE);
              _bufPos = 0;
              resultLen = BUF_SIZE;
              break;
            }
          }
        }

        while (len >= BUF_SIZE) {
          processData(inp, inOff, BUF_SIZE, out, outOff + resultLen);
          poly1305.update(out, outOff + resultLen, BUF_SIZE);
          inOff += BUF_SIZE;
          len -= BUF_SIZE;
          resultLen += BUF_SIZE;
        }

        if (len > 0) {
          utils.arrayCopy(inp, inOff, _buf, 0, len);
          _bufPos = len;
        }
        break;
      }
    default:
      throw StateError('');
  }

  return resultLen;
}