Skip to content

Commit

Permalink
Update docs on sqlcipher flutter libs
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Feb 7, 2024
1 parent eb5ff82 commit 882aad1
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'dart:ffi';

Future<void> applyWorkaroundToOpenSqlCipherOnOldAndroidVersions() async {
throw 'stub!';
}

DynamicLibrary openCipherOnAndroid() {
throw 'stub';
}
6 changes: 6 additions & 0 deletions docs/assets/sqlcipher_flutter_libs/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: sqlcipher_flutter_libs
publish_to: none
description: Fake "sqlcipher_flutter_libs" package so that we can import it in snippets without depending on Flutter.

environment:
sdk: ^2.16.0
2 changes: 1 addition & 1 deletion docs/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ targets:
global_options:
":api_index":
options:
packages: ['drift', 'drift_dev', 'sqlite3']
packages: ['drift', 'drift_dev', 'sqlite3', 'sqlite3_flutter_libs', 'sqlcipher_flutter_libs']

additional_public_assets:
- "pages/**"
20 changes: 14 additions & 6 deletions docs/lib/snippets/platforms/encryption.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import 'dart:io';
import 'package:drift/native.dart';
import 'package:drift_docs/snippets/isolates.dart';
import 'package:sqlite3/sqlite3.dart';

// #docregion setup
import 'dart:ffi';
import 'package:sqlite3/open.dart';
import 'package:sqlcipher_flutter_libs/sqlcipher_flutter_libs.dart';

// call this method before using drift
void setupSqlCipher() {
open.overrideFor(
OperatingSystem.android, () => DynamicLibrary.open('libsqlcipher.so'));
Future<void> setupSqlCipher() async {
await applyWorkaroundToOpenSqlCipherOnOldAndroidVersions();
open.overrideFor(OperatingSystem.android, openCipherOnAndroid);
}
// #enddocregion setup

Expand All @@ -23,9 +24,13 @@ void databases() {
final myDatabaseFile = File('/dev/null');

// #docregion encrypted1
final token = RootIsolateToken.instance;
NativeDatabase.createInBackground(
myDatabaseFile,
isolateSetup: setupSqlCipher,
isolateSetup: () async {
BackgroundIsolateBinaryMessenger.ensureInitialized(token);
await setupSqlCipher();
},
setup: (rawDb) {
rawDb.execute("PRAGMA key = 'passphrase';");
},
Expand All @@ -35,7 +40,10 @@ void databases() {
// #docregion encrypted2
NativeDatabase.createInBackground(
myDatabaseFile,
isolateSetup: setupSqlCipher,
isolateSetup: () async {
BackgroundIsolateBinaryMessenger.ensureInitialized(token);
await setupSqlCipher();
},
setup: (rawDb) {
assert(_debugCheckHasCipher(rawDb));
rawDb.execute("PRAGMA key = 'passphrase';");
Expand Down
15 changes: 7 additions & 8 deletions docs/pages/docs/Platforms/encryption.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ To use `sqlcipher`, add a dependency on `sqlcipher_flutter_libs`:

```yaml
dependencies:
sqlcipher_flutter_libs: ^0.5.0
sqlcipher_flutter_libs: ^0.6.0
```

If you already have a dependency on `sqlite3_flutter_libs`, __drop that dependency__.
Expand All @@ -77,14 +77,13 @@ of the regular `libsqlite3.so`:
When using drift on a background database, you need to call `setupSqlCipher` on the background isolate
as well. With `NativeDatabase.createInBackground`, which are using isolates internally, you can use
the `setupIsolate` callback to do this - the examples on this page use this as well.
Since `applyWorkaroundToOpenSqlCipherOnOldAndroidVersions()` invokes a platform channel, one needs
to install a `BackgroundIsolateBinaryMessenger` on the isolate as well.

On iOS and macOS, no additional setup is necessary - simply depend on `sqlcipher_flutter_libs`.

On Windows and Linux, you currently have to include a version of SQLCipher manually when you distribute
your app.
For more information on this, you can use the documentation [here]({{ '../Platforms/index.md#bundling-sqlite-with-your-app' | pageUrl }}).
Instead of including `sqlite3.dll` or `libsqlite3.so`, you'd include the respective versions
of SQLCipher.
On iOS, macOS and Windows, no additional setup is necessary - simply depend on `sqlcipher_flutter_libs`.
For Linux builds, note that OpenSSL is linked statically by default. If you want to compile your app to use
a dynamically-linked distribution of OpenSSL, see [this](https://github.com/simolus3/sqlite3.dart/issues/186#issuecomment-1742110933)
issue comment.

### Using

Expand Down
2 changes: 2 additions & 0 deletions docs/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ dependencies:
path: assets/path_provider
sqlite3_flutter_libs:
path: assets/sqlite3_flutter_libs
sqlcipher_flutter_libs:
path: assets/sqlcipher_flutter_libs
# Used in examples
rxdart: ^0.27.3
yaml: ^3.1.1
Expand Down

0 comments on commit 882aad1

Please sign in to comment.