encryptBytes static method

Uint8List encryptBytes(
  1. Uint8List bytes
)

Encrypts bytes with a freshly generated random IV.

Returns AES-CBC ciphertext concatenated.

Implementation

static Uint8List encryptBytes(Uint8List bytes) {
  if (bytes.isEmpty) return Uint8List(0);
  _assertInitialized();

  final rng = Random.secure();
  final ivBytes = Uint8List.fromList(
    List<int>.generate(_ivLength, (_) => rng.nextInt(256)),
  );
  final iv = encrypt.IV(ivBytes);
  final ciphertext = _encrypter!.encryptBytes(bytes, iv: iv);

  // Layout: [_ivLength bytes of IV][ciphertext bytes]
  final result = Uint8List(_ivLength + ciphertext.bytes.length);
  result.setRange(0, _ivLength, ivBytes);
  result.setRange(_ivLength, result.length, ciphertext.bytes);
  return result;
}