-
I couldn't find any information regarding support for SQLCipher. Do you know if it is possible? |
Beta Was this translation helpful? Give feedback.
Answered by
rkistner
Apr 12, 2023
Replies: 1 comment
-
It's possible, but requires some additional work at the moment. Load the SQLCipher libraries instead of SQLiteFor Flutter, use sqlcipher_flutter_libs instead of sqlite3_flutter_libs. See the linked documentation for details. For CLI Dart apps, this may be a little more involved. See this example for manually providing the compiled library path. Set the key in each connectionA custom class SqlcipherOpenFactory extends DefaultSqliteOpenFactory {
String? key;
SqlcipherOpenFactory({required super.path, super.sqliteOptions, this.key});
@override
sqlite.Database open(SqliteOpenOptions options) {
final db = super.open(options);
if (key != null) {
// Make sure that SQLCipher is used, not plain SQLite.
final versionRows = db.select('PRAGMA cipher_version');
if (versionRows.isEmpty) {
throw AssertionError(
'SQLite library is plain SQLite; SQLCipher expected.');
}
}
return db;
}
@override
List<String> pragmaStatements(SqliteOpenOptions options) {
final defaultStatements = super.pragmaStatements(options);
if (key != null) {
return [
// Set the encryption key as the first statement
"PRAGMA KEY = '$key'",
// Include the default statements afterwards
for (var statement in defaultStatements) statement
];
} else {
return defaultStatements;
}
}
} Use this factory when opening a databasefinal db = SqliteDatabase.withFactory(SqlcipherOpenFactory(path: path, key: key)); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
phillvdm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's possible, but requires some additional work at the moment.
Load the SQLCipher libraries instead of SQLite
For Flutter, use sqlcipher_flutter_libs instead of sqlite3_flutter_libs. See the linked documentation for details.
For CLI Dart apps, this may be a little more involved. See this example for manually providing the compiled library path.
Set the key in each connection
A custom
SQLiteOpenFactory
is required for this, for example: