I am using Room on Android, now I want to use SQLCipher and Room together, how to do updating.
I have meet two conditions. First is my Room Database is exist, and does not encrypt, how to do update when using SQLCipher. Second is my Room Database is encrypt, how to do updating when using SQLCipher.
I had search the posts show that if the db file is not encrypt, i should encrypt first, and then add SupportFactory, if the db file is exist and is encrypt, just set SupportFactory。Is it right if I do it as these steps?
Thansk.
UPDATE:
Finally, I reach my goal by using follow codes:
if (shouldEncrypt) {
char[] userPassphrase = dbEncryptPassword.toCharArray();
byte[] passphrase = SQLiteDatabase.getBytes(userPassphrase);
SQLCipherUtils.State state = SQLCipherUtils.getDatabaseState(context, dbName);
// If file exist and not encrypt, encrypt it first
if (state != SQLCipherUtils.State.DOES_NOT_EXIST && state != SQLCipherUtils.State.ENCRYPTED
try {
SQLCipherUtils.encrypt(context, dbName, userPassphrase);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//Add SQLCipher SupportFactory, and keep Room's migration step
SupportFactory factory = new SupportFactory(passphrase);
dbInstance = Room.databaseBuilder(
context.getApplicationContext(), IMDatabase.class, dbName)
.allowMainThreadQueries()
.addMigrations(MIGRATION_1_2)
.openHelperFactory(factory)
.build();
} else {
dbInstance = Room.databaseBuilder(
context.getApplicationContext(), IMDatabase.class, dbName)
.allowMainThreadQueries()
.addMigrations(MIGRATION_1_2)
.build();
}
I test it is work fine, but I am not sure it is strong.