doFinal method
Process the last block of data given by inp
and starting at offset inpOff
and pad it as
explained in this interface's description.
For encryption, the resulting cipher text is put in out
beginning at position outOff
and
the method returns the total bytes put in out
, including the padding. Note that, if inp
length is equal to the cipher's block size, out
will need to be twice the cipher's block size
to allow place for the padding.
For decryption, the resulting plain text is put in out
beginning at position outOff
and the
method returns the total bytes put in out
, excluding the padding. Note that the method may
return 0 if the last block was all padding.
Implementation
@override
int doFinal(Uint8List inp, int inpOff, Uint8List out, int outOff) {
if (_encrypting!) {
var lastInputBlock = Uint8List(blockSize)..setAll(0, inp.sublist(inpOff));
var remainder = inp.length - inpOff;
if (remainder < blockSize) {
// Padding goes embedded in last block of data
padding.addPadding(lastInputBlock, inp.length - inpOff);
processBlock(lastInputBlock, 0, out, outOff);
return blockSize;
} else {
// Padding goes alone in an additional block
processBlock(inp, inpOff, out, outOff);
padding.addPadding(lastInputBlock, 0);
processBlock(lastInputBlock, 0, out, outOff + blockSize);
return 2 * blockSize;
}
} else {
// Decrypt last block and remove padding
processBlock(inp, inpOff, out, outOff);
var padCount = padding.padCount(out.sublist(outOff));
var padOffsetInBlock = blockSize - padCount;
out.fillRange(outOff + padOffsetInBlock, out.length, 0);
return padOffsetInBlock;
}
}