Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Encrypted SharedPreference in Jetpack Security
// Although you can define your own key generation parameter specification, it's
// recommended that you use the value specified here.
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
val sharedPref = EncryptedSharedPreferences.create(
"preference_name",
mainKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
// PUT or Update the sharedpreference
with (sharedPref.edit()) {
putString("pref_key", "your_value")
apply()
}
// GET the sharedpreference value
val getEncryptShared = sharedPref.getString("pref_key", StringUtils.EMPTY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment