encrypt method

  1. @override
Future<int> encrypt(
  1. Uint8List inp,
  2. int inpOff,
  3. int inpLength,
  4. Uint8List out,
  5. int outOff,
)
override
  • inp: the total bytes in plain text
  • inpOff: the byte offset to start encryption at
  • inpLength: the number of bytes (length) to encrypt
  • out: the buffer to write the encrypted output in
  • outOff: the byte offset to write the encrypted output to

returns the length of the new encrypted output

Implementation

@override
Future<int> encrypt(Uint8List inp, int inpOff, int inpLength, Uint8List out,
    int outOff) async {
  /// generating new nonce / iv
  final nonce = _algorithm.newNonce();

  /// encrypt the next 16 bytes of the [inp] starting from [inpOff]
  final secretBox = await _algorithm.encrypt(
    inp.view(inpOff, inpLength),
    secretKey: await secretKey.future,
    nonce: nonce,
  );

  assert(secretBox.mac.bytes.isEmpty && secretBox.nonce.length == 16,
      'The AesCbc nonce / iv and mac should be 16 / 0 bytes long.');

  /// concentrating the encrypted text, nonce / iv and mac
  final concentration = secretBox.concatenation();

  /// save the encrypted bytes in the [out]
  final outOffset = outOff + concentration.length;
  if (outOffset > out.length) {
    final previousOut = Uint8List.fromList(out);
    out = Uint8List(outOffset)..setRange(0, previousOut.length, previousOut);
  }
  out.setRange(outOff, outOffset, concentration);

  return concentration.length;
}