@@ -22,8 +22,9 @@ import dagger.hilt.InstallIn
2222import dagger.hilt.android.qualifiers.ApplicationContext
2323import dagger.hilt.components.SingletonComponent
2424import kotlinx.coroutines.runBlocking
25- import net.sqlcipher.database.SQLiteDatabase
26- import net.sqlcipher.database.SupportFactory
25+ import net.zetetic.database.sqlcipher.SQLiteDatabase
26+ import net.zetetic.database.sqlcipher.SupportOpenHelperFactory
27+ import java.nio.charset.Charset
2728import javax.inject.Singleton
2829
2930@Module
@@ -56,14 +57,14 @@ object AppModule {
5657 }
5758 }
5859
59- SQLiteDatabase .loadLibs(context)
60+ System .loadLibrary( " sqlcipher " );
6061
6162 if (isMigratedFromNonEncryption) {
6263 context.migrateNonEncryptedToEncryptedDb(dbName, dbNameEncrypted, userEnteredPassphrase)
6364 }
6465
65- val passphrase: ByteArray = SQLiteDatabase . getBytes(userEnteredPassphrase.toCharArray() )
66- val factory = SupportFactory (passphrase)
66+ val passphrase: ByteArray? = getBytes(userEnteredPassphrase)
67+ val factory = SupportOpenHelperFactory (passphrase)
6768 builder.openHelperFactory(factory)
6869 builder.addMigrations(object : Migration (DB_VERSION_3 , DB_VERSION_4 ) {
6970 override fun migrate (database : SupportSQLiteDatabase ) {
@@ -112,7 +113,7 @@ object AppModule {
112113 private fun Context.migrateNonEncryptedToEncryptedDb (nonEncryptedDbName : String , encryptedDbName : String , userEnteredPassphrase : String ) {
113114 try {
114115 val oldDb = getDatabasePath(nonEncryptedDbName)
115- val database = SQLiteDatabase .openOrCreateDatabase(oldDb, " " , null )
116+ val database = SQLiteDatabase .openOrCreateDatabase(oldDb, " " , null , null )
116117 val encryptedDbPath = getDatabasePath(encryptedDbName).path
117118 database.rawExecSQL(
118119 " ATTACH DATABASE '$encryptedDbPath ' AS encrypted KEY '$userEnteredPassphrase '"
@@ -125,4 +126,10 @@ object AppModule {
125126 e.printStackTrace()
126127 }
127128 }
129+
130+ private fun getBytes (data : String? ): ByteArray? {
131+ return if (data != null && data.length != 0 ) data.toByteArray(Charset .forName(" UTF-8" )) else ByteArray (
132+ 0
133+ )
134+ }
128135}
0 commit comments