Saferoom migration to SQLCipher latest version
sunilsunny565 opened this issue · 2 comments
I'm trying migrate from using https://github.com/commonsguy/cwac-saferoom to SQLCipher library as Saferoom is no more maintained. I didn't had any issues with SafeRoom I was using "com.commonsware.cwac:saferoom:1.2.1" which is using SQLCipher version 4.2.0 and this is how I'm using it.
dbBuilder.openHelperFactory(SafeHelperFactory(dbKey, SafeHelperFactory.POST_KEY_SQL_V3))
//If the database hasn't been encrypted, encrypt it
if (SQLCipherUtils.getDatabaseState(context, Common.config().dbName()) == SQLCipherUtils.State.UNENCRYPTED) {
SQLCipherUtils.encrypt(context, Common.config().dbName(), dbKey)
}
Where dbBuilder is just Room.databaseBuilder
Now to to update to SQLCipher :4.5.2 replaced above code with
val factory = SupportFactory(SQLiteDatabase.getBytes(dbKey))
dbBuilder.openHelperFactory(factory)
And after this change I started to get this error
file is not a database: , while compiling: select count(*) from sqlite_master;
which I feel happens when the dbKey is different, but in this case I haven't changed the key.
Am I missing something in this migration? @commonsguy , can you please help?.
I'm just answering my own question I had to pass SQLiteDatabaseHook and that resolved the issue
val hook: SQLiteDatabaseHook = object : SQLiteDatabaseHook {
override fun preKey(database: SQLiteDatabase) {
// do nothing
}
override fun postKey(database: SQLiteDatabase?) {
database?.rawExecSQL(POST_KEY_SQL_MIGRATE)
}
}
val factory = SupportFactory(SQLiteDatabase.getBytes(dbKey), hook)
dbBuilder.openHelperFactory(factory)
}
const val POST_KEY_SQL_MIGRATE = "PRAGMA cipher_migrate;"
it might be a good idea if this can be mentioned in the wiki I guess.
We are happy to hear that you were able to solve the issue. The SQLiteDatabaseHook usage with SupportFactory is covered within the README here. If you have any other questions, please consider posted to the SQLCipher Community Discuss site as we tend to only track software defects within GitHub Issues. Thanks!