-
- All Implemented Interfaces:
-
android.content.SharedPreferences
public final class EncryptedSharedPreferences implements SharedPreferences
An implementation of SharedPreferences that encrypts keys and values. WARNING: The preference file should not be backed up with Auto Backup. When restoring the file it is likely the key used to encrypt it will no longer be present. You should exclude all
EncryptedSharedPreferences from backup using backup rules. Basic use of the class:
Based on EncryptedSharedPreferences.java from AndroidX Crypto - v1.1.0-alpha07MasterKey masterKey = new MasterKey.Builder(context) .setKeyScheme(MasterKey.KeyScheme.AES256_GCM) .build(); SharedPreferences sharedPreferences = EncryptedSharedPreferences.create( context, "secret_shared_prefs", masterKey, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM ); // use the shared preferences and editor as you normally would SharedPreferences.Editor editor = sharedPreferences.edit();Permalink: e50caac
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public enumEncryptedSharedPreferences.PrefKeyEncryptionSchemeThe encryption scheme to encrypt keys.
public enumEncryptedSharedPreferences.PrefValueEncryptionSchemeThe encryption scheme to encrypt values.
-
Method Summary
-
Methods inherited from class android.content.SharedPreferences
contains, edit, getAll, getBoolean, getFloat, getInt, getLong, getString, getStringSet, registerOnSharedPreferenceChangeListener, unregisterOnSharedPreferenceChangeListener -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
create
@NonNull() static SharedPreferences create(@NonNull() Context context, @NonNull() String fileName, @NonNull() MasterKey masterKey, @NonNull() EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme, @NonNull() EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme)
Opens an instance of encrypted SharedPreferences
- Parameters:
fileName- The name of the file to open; can not contain path separators.masterKey- The master key to use.prefKeyEncryptionScheme- The scheme to use for encrypting keys.prefValueEncryptionScheme- The scheme to use for encrypting values.
-
create
@Deprecated()@NonNull() static SharedPreferences create(@NonNull() String fileName, @NonNull() String masterKeyAlias, @NonNull() Context context, @NonNull() EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme, @NonNull() EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme)
Opens an instance of encrypted SharedPreferences
If the
masterKeyAliasused here is for a key that is not yet created, thismethod will not be thread safe. Use the alternate signature that is not deprecated formulti-threaded contexts.- Parameters:
fileName- The name of the file to open; can not contain path separators.masterKeyAlias- The alias of the master key to use.context- The context to use to open the preferences file.prefKeyEncryptionScheme- The scheme to use for encrypting keys.prefValueEncryptionScheme- The scheme to use for encrypting values.
-
getStringSet
@Nullable() Set<String> getStringSet(@Nullable() String key, @Nullable() Set<String> defValues)
-
getBoolean
boolean getBoolean(@Nullable() String key, boolean defValue)
-
edit
@NonNull() SharedPreferences.Editor edit()
-
registerOnSharedPreferenceChangeListener
void registerOnSharedPreferenceChangeListener(@NonNull() SharedPreferences.OnSharedPreferenceChangeListener listener)
-
unregisterOnSharedPreferenceChangeListener
void unregisterOnSharedPreferenceChangeListener(@NonNull() SharedPreferences.OnSharedPreferenceChangeListener listener)
-
-
-
-