Thread
#multiplatform
    John Oberhauser

    John Oberhauser

    1 year ago
    Is there a recommend way to encrypt a database in KMP? I've got SqlDelight set up, and SQL Cipher for android from here https://github.com/sqlcipher/android-database-sqlcipher But I'm not sure what to do about desktop and ios. I'm looking to be able to create a backup of one encrypted database of one platform, and decrypt it in another. edit: this looks like it'll work for JVM (desktop) https://github.com/Willena/sqlite-jdbc-crypt/tree/3.33.0.1 still not sure about ios though
    ian.shaun.thomas

    ian.shaun.thomas

    1 year ago
    I don't have an answer but I would start by looking to see if a cross platform keystore library exists for using the native platform keystore implementations which you definitely want. Swing probably has something for this as I've seen cross platform apps do this so it's likely that something exists for it.
    kpgalligan

    kpgalligan

    1 year ago
    This is old and not entirely complete, but here's the basic process for iOS. https://dev.to/samhill303/multiplatform-encryption-with-sqldelight-and-sqlcipher-5do4 . You need to avoid linking the platform sqlite implementation, include sqlcipher, then add the key in the sqldelight/sqliter driver config (this part appears to be missing from the docs).
    The sqldelight driver setup will look like this:
    val configuration = DatabaseConfiguration(
                name = "MyDb",
                version = schema.version,
                create = { connection ->
                    wrapConnection(connection) { schema.create(it) }
                },
                upgrade = { connection, oldVersion, newVersion ->
                    wrapConnection(connection) { schema.migrate(it, oldVersion, newVersion) }
                },
                key = myEncryptionKey
            )
    
            NativeSqliteDriver(
                configuration
            )