ejson method

Future<Uint8List> ejson(
  1. Uint8List jsonData,
  2. String password
)

Encrypts JSON data using AES and returns the encrypted bytes

Implementation

Future<Uint8List> ejson(Uint8List jsonData, String password) async {
  final key = _generateKey(password);
  final iv = encrypt.IV.fromLength(16);
  final encrypter =
      encrypt.Encrypter(encrypt.AES(key, mode: encrypt.AESMode.cbc));
  final encryptedChunks = <Uint8List>[];
  for (int i = 0; i < jsonData.length; i += 4096) {
    final chunk = jsonData.sublist(
        i, i + 4096 > jsonData.length ? jsonData.length : i + 4096);
    final encryptedChunk = encrypter.encryptBytes(chunk, iv: iv);
    encryptedChunks.add(Uint8List.fromList(encryptedChunk.bytes));
  }
  final result = Uint8List.fromList(
      iv.bytes + encryptedChunks.expand((e) => e).toList());
  return result;
}