Skip to content

Instantly share code, notes, and snippets.

@AliAzaz
Created October 9, 2021 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliAzaz/c29c76ca224fb980201dbf518a2f1dac to your computer and use it in GitHub Desktop.
Save AliAzaz/c29c76ca224fb980201dbf518a2f1dac to your computer and use it in GitHub Desktop.
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