encrypt method
Encrypts src block-by-block in ECB mode, padding first if
paddingStyle is set. Writes into dst if provided (dst must
be exactly the padded input length).
Implementation
@override
BuBytes encrypt(List<int> src, [List<int>? dst]) {
final paddingStyle = this.paddingStyle;
if (paddingStyle == null) {
if ((src.length % blockSize) != 0) {
throw CoreErrors.invalidOperationArguments(
"encrypt",
name: "src",
reason: "Invalid source bytes length.",
);
}
}
List<int> input = src.clone();
if (paddingStyle != null) {
input = BlockCipherPadding.pad(input, blockSize, style: paddingStyle);
}
final out = BuBytes.unsafeMutable(dst ?? List<int>.filled(input.length, 0));
if (out.length != input.length) {
throw CoreErrors.invalidOperationArguments(
"encrypt",
name: "dst",
reason: "Incorrect destination length.",
);
}
final numBlocks = input.length ~/ blockSize;
for (var i = 0; i < numBlocks; i++) {
final start = i * blockSize;
final end = (i + 1) * blockSize;
final List<int> block = input.sublist(start, end);
final enc = super.encryptBlock(block);
out.setRange(start, end, enc);
}
return out;
}