Created
December 7, 2023 20:59
-
-
Save sergei-mikhailovskii-idf/30ba33eb1ed11ffa483bfff1ba89d5bd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
actual fun getNonEncryptedSettings(context: AppContext?): Settings = SharedPreferencesSettings( | |
PreferenceManager.getDefaultSharedPreferences(context) | |
) | |
actual fun getEncryptedSettings(context: AppContext?): Settings { | |
requireNotNull(context) | |
val masterKey = MasterKey.Builder(context) | |
.setKeyScheme(KeyScheme.AES256_GCM) | |
.build() | |
val encryptedPreferences = EncryptedSharedPreferences.create( | |
context, | |
context.packageName + "_encrypted_preferences", | |
masterKey, | |
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | |
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | |
) | |
val oldPreferences = getNonEncryptedSettings(context) | |
return SharedPreferencesSettings(MigrationPreferences(listOf("key1", "key2"), oldPreferences, encryptedPreferences)) | |
} | |
class MigrationPreferences( | |
keysToMigrate: List<String>, | |
oldPreferences: Settings, | |
private val encryptedPreferences: SharedPreferences | |
) : SharedPreferences by encryptedPreferences { | |
init { | |
encryptedPreferences.edit { | |
keysToMigrate.forEach { key -> | |
val value = oldPreferences.getStringOrNull(key) | |
value?.let { | |
putString(key, it) | |
oldPreferences.remove(key) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment