Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Encrypted Room db
abstract class EncryptedDatabase : RoomDatabase() {
companion object {
fun getInstance(passcode: CharArray, context: Context):
EncryptedDatabase = buildDatabase(passcode, context)
private fun buildDatabase(
passcode: CharArray,
context: Context
): EncryptedDatabase {
// DatabaseKeyMgr is a singleton that all of the above code is wrapped into.
// Ideally this should be injected through DI but to simplify the sample code
// we'll retrieve it as follows
val dbKey = DatabaseKeyMgr.getInstance().getCharKey(passcode, context)
val supportFactory = SupportFactory(SQLiteDatabase.getBytes(dbKey))
return Room.databaseBuilder(context, EncryptedDatabase::class.java,
"encrypted-db").openHelperFactory(supportFactory).build()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.