HI ! I need to store Key-Value info in a multiplat...
# multiplatform
m
HI ! I need to store Key-Value info in a multiplatform project, basically api keys, codes and refresh tokens, which library would you recomend me ? I’m trying to access it from the Common part, i’m trying to use native UI but put everything else in the common module
m
m
is it secure for my purpose ?
m
And check this one SQLDelight => https://cashapp.github.io/sqldelight/2.0.1/
SQLDelight: is an actual SQLite database (take care when using it for JS) multiplatform-settings: key-value data which is different per platform • Apple uses UserDefaults • Android uses SharedPreferences • JVM uses PropertiesSettings • JS uses Storage • Windows uses RegistrySettings
r
Multiplatform settings supports encryption delegates (e.g. encrypted shared preference)
y
Ya its secure it will encrypte your values
m
i think i will use “multiplatform settings”, is there any example or sample ? what i found is half exmplained, looking for a real project, i’m struggling to pass the delegates
y
Sure i can explain it to u lets hop on a call real quick i sent u a dm
r
This is how I do it in my project
iosMain:
Copy code
import com.russhwolf.settings.ExperimentalSettingsImplementation
import com.russhwolf.settings.KeychainSettings
Copy code
actual class EncryptedSettingsFactory(
) {
    @OptIn(ExperimentalSettingsImplementation::class)
    actual fun create(): EncryptedSettingsRepository {
Copy code
return EncryptedSettingsRepository(
            KeychainSettings(service = "ENCRYPTED_SESSION_SETTINGS")
        )
    }
}
Copy code
private val encryptedSettingsRepository: EncryptedSettingsRepository by lazy {
        EncryptedSettingsFactory().create()
    }
androidMain:
Copy code
import android.content.SharedPreferences
import com.russhwolf.settings.SharedPreferencesSettings
Copy code
actual class EncryptedSettingsFactory(
    private val sharedPrefs: SharedPreferences
) {
    actual fun create(): EncryptedSettingsRepository {
Copy code
return EncryptedSettingsRepository(
            SharedPreferencesSettings(sharedPrefs)
        )
    }
}
Copy code
fun  Application.createEncryptedSettingsRepository(): EncryptedSettingsRepository {
    val masterKey = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
    val encryptedPrefs = EncryptedSharedPreferences.create(
        /* fileName = */ "ENCRYPTED_SESSION_SETTINGS",
        /* masterKeyAlias = */ masterKey,
        /* context = */ this,
        /* prefKeyEncryptionScheme = */ EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
        /* prefValueEncryptionScheme = */ EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
    )
    return EncryptedSettingsFactory(
        encryptedPrefs
    ).create()
}
Copy code
private val encryptedSettingsRepository: EncryptedSettingsRepository by lazy {
        context.createEncryptedSettingsRepository()
    }
commonMain
Copy code
expect class EncryptedSettingsFactory {
    fun create(): EncryptedSettingsRepository
}
Copy code
class EncryptedSettingsRepository(
    private val settings: Settings
) {
Copy code
fun setMyString(myString: String) {
            settings.putString("my_string", myString)
    }
fun setMyString(myString: String): String? {
            settings.getStringOrNull("my_string")
    }
}
r
Let me know if there's things that are confusing in the docs (readme) or sample app
m