encrypt method
inp
: the total bytes in plain textinpOff
: the byte offset to start encryption atinpLength
: the number of bytes (length) to encryptout
: the buffer to write the encrypted output inoutOff
: 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;
}