Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The encryption part is not working properly #94

Open
DongXinyu opened this issue Feb 13, 2023 · 10 comments
Open

The encryption part is not working properly #94

DongXinyu opened this issue Feb 13, 2023 · 10 comments

Comments

@DongXinyu
Copy link

The following code works fine when kotlin("multiplatform").version("1.7.10"), and the encryption part is abnormal when kotlin("multiplatform").version("1.8.0").

   val schema = MyRecordDB.Schema
    val databaseConfiguration = DatabaseConfiguration(
      name = "mytest.db",
      version = schema.version,
      create = { connection ->
        wrapConnection(connection) { schema.create(it) }
      },
      upgrade = { connection, oldVersion, newVersion ->
        wrapConnection(connection) {
          schema.migrate(it, oldVersion, newVersion)
        }
      },
      encryptionConfig = DatabaseConfiguration.Encryption(key = "ABC")
    )
    val driver = NativeSqliteDriver(databaseConfiguration)
    MyDatabaseWrapper(MyRecordDB(driver))

The main problem is the following code, and the rest of the code works fine.

encryptionConfig = DatabaseConfiguration.Encryption(key = "ABC")
@kpgalligan
Copy link
Contributor

What is "abnormal"?

@DongXinyu
Copy link
Author

@kpgalligan I'm sorry to have troubled you. What I'm trying to say is not working properly.

@kpgalligan
Copy link
Contributor

In what way? Doesn't compile? It's not encrypting the data?

@DongXinyu
Copy link
Author

@kpgalligan
It compiles normally. When running on an iPhone, the data is not encrypted. When running in the emulator, the database cannot be opened using the key in the code. I downloaded the generated database to my computer and then tried to open it with DB Browser for SQLite.

@kpgalligan
Copy link
Contributor

That has nothing to do with sqliter. That's how you're linking to SQLCipher (or whatever you're using).

@DongXinyu
Copy link
Author

DongXinyu commented Feb 13, 2023

I installed SQLCipher correctly.

Call DatabaseConfiguration encryption is normal when Kotlin is set to 1.7.10.

// Project/build.gradle.kts
plugins {
    //trick: for the same plugin versions in all sub-modules
    id("com.android.application").version("7.4.1").apply(false)
    id("com.android.library").version("7.4.1").apply(false)
    kotlin("android").version("1.7.10").apply(false)
    kotlin("multiplatform").version("1.7.10").apply(false)
}

DatabaseConfiguration encryption goes wrong when Kotlin is set to 1.8.0.

// Project/build.gradle.kts
plugins {
    //trick: for the same plugin versions in all sub-modules
    id("com.android.application").version("7.4.1").apply(false)
    id("com.android.library").version("7.4.1").apply(false)
    kotlin("android").version("1.8.0").apply(false)
    kotlin("multiplatform").version("1.8.0").apply(false)
}

1

Android Studio prompts DatabaseConfiguration here:

https://github.com/touchlab/SQLiter/blob/main/sqliter-driver/src/nativeCommonMain/kotlin/co/touchlab/sqliter/DatabaseConfiguration.kt

@DongXinyu
Copy link
Author

You mean SQLCipher does not support Kotlin 1.8.0, not related to DatabaseConfiguration, am I understanding correctly?

@kpgalligan
Copy link
Contributor

I mean that if you link to sqlite on iOS, you can pass in an encryption key but it won't matter. You need to bundle and link to SQLCipher. It is very hard to figure anything out without seeing your whole config, but basically you need to confirm that you aren't linking to the included sqlite.

How and when do you include SQLCipher on iOS?

@DongXinyu
Copy link
Author

My project uses Kotlin Mulitplatform Mobile and SqlDelight. SQLCipher is used to enhance SQLite for encryption.

I think touchlabSQLiter is the equivalent of middleware, am I getting that right?

Kotlin Mulitplatform Mobile
├── androidMain
├── commonMain
└── iosMain
    └── SqlDelight
        └── touchlabSQLiter
            └── SQLCipher
                └── SQLite

Some of my configurations are as follows:

# project/bulid.gradle.kts
plugins {
    id("com.android.application").version("7.4.1").apply(false)
    id("com.android.library").version("7.4.1").apply(false)
    kotlin("android").version("1.7.10").apply(false)
    /* The encryption problem occurred in 1.8.0 */
    kotlin("multiplatform").version("1.7.10").apply(false)
}

buildscript {
    dependencies {
        classpath("com.squareup.sqldelight:gradle-plugin:1.5.5")
    }
}
# shared/build.gradle.kts
plugins {
    kotlin("multiplatform")
    kotlin("native.cocoapods")
    id("com.android.library")
    id("com.squareup.sqldelight")
}

    cocoapods {
        summary = "Some description for the Shared Module"
        homepage = "Link to the Shared Module homepage"
        version = "1.0.1"
        ios.deploymentTarget = "14.1"
        podfile = project.file("../iosApp/Podfile")
        framework {
            baseName = "shared"
        }
        pod("SQLCipher") { /* Call Cocoapods pod install here to install SQLCipher4.5.3 */
            version = "~> 4.5.3"
        }
    }

    sourceSets {
        val iosArm64Main by getting
        val iosX64Main by getting
        val iosSimulatorArm64Main by getting
        val iosMain by creating {
            dependsOn(commonMain)
            iosArm64Main.dependsOn(this)
            iosX64Main.dependsOn(this)
            iosSimulatorArm64Main.dependsOn(this)
            dependencies {
                implementation("com.squareup.sqldelight:native-driver:1.5.5")
                implementation("co.touchlab:sqliter-driver:1.2.1")
            }
        }

Test equipment:

  1. iPhone Emulator (iOS15 iPhone8Plus)
  2. iPhone Real (iOS15.7.2 iPhone7Plus)
  3. iPhone Real (iOS16.3 iPhone14Pro)

@DongXinyu
Copy link
Author

DongXinyu commented Feb 14, 2023

# shared/iosMain/Kotlin/actual.kt

package me.dxy.common.repository

import co.touchlab.sqliter.DatabaseConfiguration
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
import com.squareup.sqldelight.drivers.native.wrapConnection
import me.dxy.common.di.MyTestDatabaseWrapper
import me.dxy.mytest.shared.db.MyTestDB
import org.koin.dsl.module

actual fun platformModule() = module {
  single {
    val schema = MyTestDB.Schema
    val databaseConfiguration = DatabaseConfiguration(
      name = "testEncrypt.db",
      version = schema.version,
      create = { connection ->
        wrapConnection(connection) { schema.create(it) }
      },
      upgrade = { connection, oldVersion, newVersion ->
        wrapConnection(connection) {
          schema.migrate(it, oldVersion, newVersion)
        }
      },
      encryptionConfig = DatabaseConfiguration.Encryption("123")
    )
    val driver = NativeSqliteDriver(databaseConfiguration)
    MyTestDatabaseWrapper(MyTestDB(driver))
  }
}
# iosApp/iOSApp.swfit

import SwiftUI
import shared
import SQLCipher

@main
struct iOSApp: App {
    init() {
        KoinKt.doInitKoin()
    }
	var body: some Scene {
		WindowGroup {
			ContentView()
		}
	}
}
# iosApp/ContentView.swfit

import SwiftUI
import shared
import SQLCipher

struct ContentView: View {

	var body: some View {
		Text("Oh yeah!")
                Button("Click me") {
                    MyTestRepository().savePeople()
                }
	}
}

struct ContentView_Previews: PreviewProvider {
	static var previews: some View {
		ContentView()
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants