Skip to content

Commit 32e23c9

Browse files
Merge pull request #1233 from yogeshpaliyal/migrateSqlCipher
Update SQLCipher integration and migration logic
2 parents 02a1a54 + 12f5308 commit 32e23c9

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

common/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ dependencies {
7272
implementation("androidx.test.ext:junit-ktx:1.2.1")
7373
androidTestApi("androidx.test:rules:1.6.1")
7474

75-
implementation("net.zetetic:android-database-sqlcipher:4.5.4")
75+
implementation("net.zetetic:sqlcipher-android:4.7.2@aar")
7676
implementation("androidx.sqlite:sqlite:2.5.1")
7777

7878
api("com.opencsv:opencsv:5.11")

common/src/main/java/com/yogeshpaliyal/common/di/module/AppModule.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import dagger.hilt.InstallIn
2222
import dagger.hilt.android.qualifiers.ApplicationContext
2323
import dagger.hilt.components.SingletonComponent
2424
import 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
2728
import 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

Comments
 (0)