touchlab / SQLiter

Minimal multiplatform sqlite library
https://touchlab.co
173 stars 35 forks source link

The encryption part is not working properly #94

Open DongXinyu opened 1 year ago

DongXinyu commented 1 year ago

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 commented 1 year ago

What is "abnormal"?

DongXinyu commented 1 year ago

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

kpgalligan commented 1 year ago

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

DongXinyu commented 1 year ago

@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 commented 1 year ago

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

DongXinyu commented 1 year ago

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 commented 1 year ago

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

kpgalligan commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago
# 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()
    }
}