Skip to content

Instantly share code, notes, and snippets.

@suryaviyyapu
Created September 7, 2020 17:30
Show Gist options
  • Save suryaviyyapu/d8f93265b6df6f935858cde93c10eca1 to your computer and use it in GitHub Desktop.
Save suryaviyyapu/d8f93265b6df6f935858cde93c10eca1 to your computer and use it in GitHub Desktop.
Android Encrypted Shared Preferences implementation using Androidx
MasterKey masterKey = null;
SharedPreferences sharedPreferences = null;
String PREFS_NAME = "PREFS_NAME";
try {
masterKey = new MasterKey.Builder(getApplicationContext(), MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build();
sharedPreferences = EncryptedSharedPreferences.create(
getApplicationContext(),
PREFS_NAME,
masterKey,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
);
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment