A post was split to a new topic: Issue Migrating From android-database-sqlcipher:3.5.9 to sqlcipher-android:4.5.5
Hi @developernotes ,
Iām trying to migrate db from net.zetetic:android-database-sqlcipher:3.5.9 to net.zetetic:sqlcipher-android:4.5.7 and the hook in the latter is providing SQLiteConnection instead of SQLiteDatabase in preKey() and postKey().
Could you help me on getting the database in postKey and handling the migration?
Hi @Gokulnath,
Here [1] is an example of migrating a SQLCipher database to the latest version using the sqlcipher-android
library.
Is " Option 2: Backwards Compatibility" would work nowadays for migrating from net.zetetic:android-database-sqlcipher:3.5.7 to net.zetetic:android-database-sqlcipher:4.5.4?
Hi @FrozenPyrozen,
Yes, you could use the compatibility PRAGMA to upgrade your library to 4.5.4 and access a SQLCipher 3.x database.
Iām not a native android dev so I wanted to check should I use it like this?
// DatabaseHelper.java
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
// Apply PRAGMAs to maintain SQLCipher 3 compatibility
db.execSQL("PRAGMA cipher_compatibility = 3;");
db.execSQL("PRAGMA cipher_page_size = 1024;");
db.execSQL("PRAGMA kdf_iter = 64000;");
db.execSQL("PRAGMA cipher_hmac_algorithm = HMAC_SHA1;");
db.execSQL("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;");
}
Hi @FrozenPyrozen,
No, if you are using the android-database-sqlcipher
library, you will want to use the SQLiteDatabaseHook
and set your compatibility via the postKey
event:
SQLiteDatabase hook = new SQLiteDatabaseHook() {
@Override
public void preKey(SQLiteDatabase database) {}
@Override
public void postKey(SQLiteDatabase database) {
database.execSQL("PRAGMA cipher_compatibility = 3;");
}
};
You can then provide the hook
parameter as an argument when opening the database connection. This will guarantee that the compatibility setting is in place before key derivation or any other SQL operation occurs.
Thanks, how to understand where to place a hook?
Hi @FrozenPyrozen,
The hook can be provided to the static openDatabase
/openOrCreateDatabase
methods found on the SQLiteDatabase
class, or if youāre using a subclass to the SQLiteOpenHelper
.
I was looking into MigrateDatabaseFrom3xFormatToCurrentFormatTest as an example, and Iāve seen some issues with SQLiteConnection
on my Android studio so Iāve created a helper method
public static SQLiteDatabaseHook migrateDbFromSupportHook() {
return new SQLiteDatabaseHook() {
@Override
public void preKey(SQLiteDatabase database) {}
@Override
public void postKey(SQLiteDatabase database) {
// Apply PRAGMAs to maintain SQLCipher 3 compatibility for net.zetetic:android-database-sqlcipher after 4x version
database.execSQL("PRAGMA cipher_compatibility = 3;");
database.execSQL("PRAGMA cipher_page_size = 1024;");
database.execSQL("PRAGMA kdf_iter = 64000;");
database.execSQL("PRAGMA cipher_hmac_algorithm = HMAC_SHA1;");
database.execSQL("PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;");
}
};
}
and passing this hook into every openDatabase
method calling in project. Is this okay?
Hi @FrozenPyrozen,
Once you set the compatibility version you donāt need to set the other pragma items as they are set on your behalf. See the implementation for reference.
Iāve got dew questions:
- Everything above, but only with
database.execSQL("PRAGMA cipher_compatibility = 3;");
would be enough? - Iāve got
import net.sqlcipher.database.SQLiteException;
and error āCannot resolve symbol āSQLiteExceptionāā. DoesSQLiteException
moved somewhere in newer versions?
Correct.
In the SQLCipher for Android 4.5.4 release [1], Android Database SQLite Exception classes are now used for compatibility with Android Support and Room.
Where I could find an info what need to be changed there?
error: cannot access SupportSQLiteDatabase SQLiteDatabase.loadLibs(context);
SQLiteDatabase imported from import net.sqlcipher.database.SQLiteDatabase;
This error is not visible on Android studio, only on CI/CD when creating build
Hi @FrozenPyrozen,
That sounds like a project configuration issue, or possibly your environment. SupportSQLiteDatabase
is not part of SQLCipher for Android [1].