https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
j

John Oberhauser

02/23/2021, 5:15 AM
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
i

ian.shaun.thomas

02/23/2021, 1:41 PM
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.
k

kpgalligan

02/23/2021, 6:17 PM
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:
Copy code
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
        )
13 Views