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

The encryption scheme to encrypt keys.

The encryption scheme to encrypt values.

Public methods

boolean
static @NonNull 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

static @NonNull SharedPreferences
create(
    @NonNull String fileName,
    @NonNull String masterKeyAlias,
    @NonNull Context context,
    @NonNull EncryptedSharedPreferences.PrefKeyEncryptionScheme prefKeyEncryptionScheme,
    @NonNull EncryptedSharedPreferences.PrefValueEncryptionScheme prefValueEncryptionScheme
)

This method is deprecated.

Use create instead.

@NonNull SharedPreferences.Editor
@NonNull Map<StringObject>
boolean
getBoolean(@Nullable String key, boolean defValue)
float
getFloat(@Nullable String key, float defValue)
int
getInt(@Nullable String key, int defValue)
long
getLong(@Nullable String key, long defValue)
@Nullable String
@Nullable Set<String>
void
void

Public methods

contains

Added in 1.0.0
public boolean contains(@Nullable String key)

create

Added in 1.1.0-alpha07
public static @NonNull 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
@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

create

Added in 1.0.0
Deprecated in 1.1.0-alpha07
public static @NonNull 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 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

edit

Added in 1.0.0
public @NonNull SharedPreferences.Editor edit()

getAll

Added in 1.0.0
public @NonNull Map<StringObjectgetAll()

getBoolean

Added in 1.0.0
public boolean getBoolean(@Nullable String key, boolean defValue)

getFloat

Added in 1.0.0
public float getFloat(@Nullable String key, float defValue)

getInt

Added in 1.0.0
public int getInt(@Nullable String key, int defValue)

getLong

Added in 1.0.0
public long getLong(@Nullable String key, long defValue)

getString

Added in 1.0.0
public @Nullable String getString(@Nullable String key, @Nullable String defValue)

getStringSet

Added in 1.0.0
public @Nullable Set<StringgetStringSet(@Nullable String key, @Nullable Set<String> defValues)

registerOnSharedPreferenceChangeListener

Added in 1.0.0
public void registerOnSharedPreferenceChangeListener(
    @NonNull SharedPreferences.OnSharedPreferenceChangeListener listener
)

unregisterOnSharedPreferenceChangeListener

Added in 1.0.0
public void unregisterOnSharedPreferenceChangeListener(
    @NonNull SharedPreferences.OnSharedPreferenceChangeListener listener
)