EncryptedSharedPreferences
class EncryptedSharedPreferences : 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 functions |
|
|---|---|
Boolean |
|
java-static SharedPreferences |
Opens an instance of encrypted SharedPreferences |
java-static SharedPreferences |
This function is deprecated. Use |
SharedPreferences.Editor |
|
(Mutable)Map<String!, Any!> |
|
Boolean |
|
Float |
|
Int |
|
Long |
|
String? |
|
(Mutable)Set<String!>? |
|
Unit |
|
Unit |
Public functions
java-static funcreate(
context: Context,
fileName: String,
masterKey: MasterKey,
prefKeyEncryptionScheme: EncryptedSharedPreferences.PrefKeyEncryptionScheme,
prefValueEncryptionScheme: EncryptedSharedPreferences.PrefValueEncryptionScheme
): SharedPreferences
Opens an instance of encrypted SharedPreferences
| Parameters | |
|---|---|
fileName: String |
The name of the file to open; can not contain path separators. |
masterKey: MasterKey |
The master key to use. |
prefKeyEncryptionScheme: EncryptedSharedPreferences.PrefKeyEncryptionScheme |
The scheme to use for encrypting keys. |
prefValueEncryptionScheme: EncryptedSharedPreferences.PrefValueEncryptionScheme |
The scheme to use for encrypting values. |
| Returns | |
|---|---|
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 |
java-static funcreate(
fileName: String,
masterKeyAlias: String,
context: Context,
prefKeyEncryptionScheme: EncryptedSharedPreferences.PrefKeyEncryptionScheme,
prefValueEncryptionScheme: EncryptedSharedPreferences.PrefValueEncryptionScheme
): SharedPreferences
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 | |
|---|---|
fileName: String |
The name of the file to open; can not contain path separators. |
masterKeyAlias: String |
The alias of the master key to use. |
context: Context |
The context to use to open the preferences file. |
prefKeyEncryptionScheme: EncryptedSharedPreferences.PrefKeyEncryptionScheme |
The scheme to use for encrypting keys. |
prefValueEncryptionScheme: EncryptedSharedPreferences.PrefValueEncryptionScheme |
The scheme to use for encrypting values. |
| Returns | |
|---|---|
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 |
funregisterOnSharedPreferenceChangeListener(
listener: SharedPreferences.OnSharedPreferenceChangeListener
): Unit