EncryptedSharedPreferences
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:
MasterKey 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();Summary
Nested types |
|---|
|
This enum is deprecated. Use |
|
This enum is deprecated. Use |
Public methods |
|
|---|---|
boolean |
|
static @NonNull SharedPreferences |
Opens an instance of encrypted SharedPreferences |
static @NonNull SharedPreferences |
This method is deprecated. Use |
@NonNull SharedPreferences.Editor |
|
@NonNull Map<String, Object> |
|
boolean |
|
float |
|
int |
|
long |
|
@Nullable String |
|
@Nullable Set<String> |
|
void |
|
void |
Public methods
public static @NonNull SharedPreferencescreate(
@NonNull Context context,
@NonNull String fileName,
@NonNull MasterKey masterKey,
@NonNull EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme,
@NonNull EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme
)
Opens an instance of encrypted SharedPreferences
| Parameters | |
|---|---|
@NonNull String fileName |
The name of the file to open; can not contain path separators. |
@NonNull MasterKey masterKey |
The master key to use. |
@NonNull EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme |
The scheme to use for encrypting keys. |
@NonNull EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme |
The scheme to use for encrypting values. |
| Returns | |
|---|---|
@NonNull SharedPreferences |
The SharedPreferences instance that encrypts all data. |
| Throws | |
|---|---|
java.security.GeneralSecurityException |
when a bad master key or keyset has been attempted |
java.io.IOException |
when fileName can not be used |
public static @NonNull SharedPreferencescreate(
@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 masterKeyAlias used here is for a key that is not yet created, this method will not be thread safe. Use the alternate signature that is not deprecated for multi-threaded contexts.
| Parameters | |
|---|---|
@NonNull String fileName |
The name of the file to open; can not contain path separators. |
@NonNull String masterKeyAlias |
The alias of the master key to use. |
@NonNull Context context |
The context to use to open the preferences file. |
@NonNull EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme |
The scheme to use for encrypting keys. |
@NonNull EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme |
The scheme to use for encrypting values. |
| Returns | |
|---|---|
@NonNull SharedPreferences |
The SharedPreferences instance that encrypts all data. |
| Throws | |
|---|---|
java.security.GeneralSecurityException |
when a bad master key or keyset has been attempted |
java.io.IOException |
when fileName can not be used |
public voidregisterOnSharedPreferenceChangeListener(
@NonNull SharedPreferences.OnSharedPreferenceChangeListener listener
)
public voidunregisterOnSharedPreferenceChangeListener(
@NonNull SharedPreferences.OnSharedPreferenceChangeListener listener
)