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

The encryption scheme to encrypt keys.

The encryption scheme to encrypt values.

Public functions

Boolean
java-static SharedPreferences
create(
    context: Context,
    fileName: String,
    masterKey: MasterKey,
    prefKeyEncryptionScheme: EncryptedSharedPreferences.PrefKeyEncryptionScheme,
    prefValueEncryptionScheme: EncryptedSharedPreferences.PrefValueEncryptionScheme
)

Opens an instance of encrypted SharedPreferences

java-static SharedPreferences
create(
    fileName: String,
    masterKeyAlias: String,
    context: Context,
    prefKeyEncryptionScheme: EncryptedSharedPreferences.PrefKeyEncryptionScheme,
    prefValueEncryptionScheme: EncryptedSharedPreferences.PrefValueEncryptionScheme
)

This function is deprecated.

Use create instead.

SharedPreferences.Editor
(Mutable)Map<String!, Any!>
Boolean
getBoolean(key: String?, defValue: Boolean)
Float
getFloat(key: String?, defValue: Float)
Int
getInt(key: String?, defValue: Int)
Long
getLong(key: String?, defValue: Long)
String?
getString(key: String?, defValue: String?)
(Mutable)Set<String!>?
getStringSet(key: String?, defValues: (Mutable)Set<String!>?)
Unit
Unit

Public functions

contains

Added in 1.0.0
fun contains(key: String?): Boolean

create

Added in 1.1.0-alpha07
java-static fun create(
    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

create

Added in 1.0.0
Deprecated in 1.1.0-alpha07
java-static fun create(
    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

edit

Added in 1.0.0
fun edit(): SharedPreferences.Editor

getAll

Added in 1.0.0
fun getAll(): (Mutable)Map<String!, Any!>

getBoolean

Added in 1.0.0
fun getBoolean(key: String?, defValue: Boolean): Boolean

getFloat

Added in 1.0.0
fun getFloat(key: String?, defValue: Float): Float

getInt

Added in 1.0.0
fun getInt(key: String?, defValue: Int): Int

getLong

Added in 1.0.0
fun getLong(key: String?, defValue: Long): Long

getString

Added in 1.0.0
fun getString(key: String?, defValue: String?): String?

getStringSet

Added in 1.0.0
fun getStringSet(key: String?, defValues: (Mutable)Set<String!>?): (Mutable)Set<String!>?

registerOnSharedPreferenceChangeListener

Added in 1.0.0
fun registerOnSharedPreferenceChangeListener(
    listener: SharedPreferences.OnSharedPreferenceChangeListener
): Unit

unregisterOnSharedPreferenceChangeListener

Added in 1.0.0
fun unregisterOnSharedPreferenceChangeListener(
    listener: SharedPreferences.OnSharedPreferenceChangeListener
): Unit