encryptBytes function

String encryptBytes({
  1. required List<int> bytes,
  2. required File outputFile,
  3. String? encryptionKey,
})

Encrypt the given bytes to the given outputFile, and return the encryption key.

Implementation

String encryptBytes({
  required final List<int> bytes,
  required final File outputFile,
  final String? encryptionKey,
}) {
  final actualEncryptionKey = encryptionKey ?? generateEncryptionKey();
  final key = Key.fromBase64(actualEncryptionKey);
  final iv = IV.fromLength(16);
  final encrypter = Encrypter(AES(key));
  final data = encrypter
      .encryptBytes(
        bytes,
        iv: iv,
      )
      .bytes;
  outputFile.writeAsBytesSync(data);
  return actualEncryptionKey;
}