drift icon indicating copy to clipboard operation
drift copied to clipboard

Pre-populated encrypted database with compression ??

Open rnik12 opened this issue 2 years ago • 1 comments

Is your feature request related to a problem? Please describe.

To prevent stealing of data, it is better to ship apps with encrypted databases. However, encrypted databases are not compressed.

A non encrypted db of size 50MB can be compressed to 10MB size using gzip.

What strategy do you suggest to ship apps with encrypted + compressed data ?

Describe the solution you'd like

  1. Pack Compressed + Encrypted sql dump into assets folder.

  2. When initialising the app, create encrypted db from the dump.

  3. I am also using FTS5 to provide offline FTS for the app. Is it advised to create 500MB db with mainly FTS5 tables on the native devices ? Or better to ship pre populated data ? How to handle compression then ?

Current Libs

  drift: ^2.9.0
  sqlcipher_flutter_libs: ^0.5.6
  sqlite3: ^2.0.0

rnik12 avatar Jun 29 '23 07:06 rnik12

Pack Compressed + Encrypted sql dump into assets folder.

That's a step you have to implement yourself.

When initialising the app, create encrypted db from the dump.

LazyDatabase(() async {
  final File dbFile = await pathToMyDatabaseFile();
  if (!await dbFile.exists()) {
    await dbFile.create();
    final data = await rootBundle.load('initial_db.db.gz');
    final asBytes = data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
    await Stream<List<int>>.value(asBytes).transform(GZipCodec().decoder).pipe(file.openWrite());
  }

  return await NativeDatabase.createInbackground(dbFile, setup: yourCallbackToSetEncryptionKeys);
});

Is it advised to create 500MB db with mainly FTS5 tables on the native devices

500MB of app data is a lot, whether you can do that depends on the expectations your users have on the size consumed by your app. If it consumes less data, shipping prep-populated indexes may be better but I don't fully understand what you mean with that.

simolus3 avatar Jun 29 '23 15:06 simolus3