public class SecurePreferences
extends java.lang.Object
implements android.content.SharedPreferences
SharedPreferences interface, which adds a
layer of encryption to the persistent storage and retrieval of sensitive
key-value pairs of primitive data types.
This class provides important - but nevertheless imperfect - protection against simple attacks by casual snoopers. It is crucial to remember that even encrypted data may still be susceptible to attacks, especially on rooted devices
Recommended to use with user password, in which case the key will be derived from the password and not stored in the file. TODO: Handle OnSharedPreferenceChangeListener
| Modifier and Type | Class and Description |
|---|---|
class |
SecurePreferences.Editor
Wrapper for Android's
SharedPreferences.Editor. |
| Constructor and Description |
|---|
SecurePreferences(android.content.Context context)
User password defaults to app generated password that's stores obfucated with the other preference values.
|
SecurePreferences(android.content.Context context,
AesCbcWithIntegrity.SecretKeys secretKey,
java.lang.String sharedPrefFilename) |
SecurePreferences(android.content.Context context,
java.lang.String password,
java.lang.String sharedPrefFilename) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
contains(java.lang.String key) |
void |
destroyKeys()
nulls in memory keys
|
SecurePreferences.Editor |
edit() |
java.util.Map<java.lang.String,java.lang.String> |
getAll() |
boolean |
getBoolean(java.lang.String key,
boolean defaultValue) |
float |
getFloat(java.lang.String key,
float defaultValue) |
int |
getInt(java.lang.String key,
int defaultValue) |
long |
getLong(java.lang.String key,
long defaultValue) |
java.lang.String |
getString(java.lang.String key,
java.lang.String defaultValue) |
java.util.Set<java.lang.String> |
getStringSet(java.lang.String key,
java.util.Set<java.lang.String> defaultValues) |
java.lang.String |
getUnencryptedString(java.lang.String key,
java.lang.String defaultValue)
Added to get a values as as it can be useful to store values that are
already encrypted and encoded
|
void |
handlePasswordChange(java.lang.String newPassword,
android.content.Context context)
Cycle through the unencrypt all the current prefs to mem cache, clear, then encypt with key generated from new password.
|
static java.lang.String |
hashPrefKey(java.lang.String prefKey)
The Pref keys must be same each time so we're using a hash to obscure the stored value
|
static boolean |
isLoggingEnabled() |
void |
registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener) |
void |
registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener,
boolean decryptKeys) |
static void |
setLoggingEnabled(boolean loggingEnabled) |
void |
unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener) |
public SecurePreferences(android.content.Context context)
context - should be ApplicationContext not Activitypublic SecurePreferences(android.content.Context context,
java.lang.String password,
java.lang.String sharedPrefFilename)
context - should be ApplicationContext not Activitypassword - user password/code used to generate encryption key.sharedPrefFilename - name of the shared pref file. If null use the default shared prefspublic SecurePreferences(android.content.Context context,
AesCbcWithIntegrity.SecretKeys secretKey,
java.lang.String sharedPrefFilename)
context - should be ApplicationContext not ActivitysecretKey - that you've generatedsharedPrefFilename - name of the shared pref file. If null use the default shared prefspublic void destroyKeys()
public static java.lang.String hashPrefKey(java.lang.String prefKey)
prefKey - public java.util.Map<java.lang.String,java.lang.String> getAll()
getAll in interface android.content.SharedPreferencespublic java.lang.String getString(java.lang.String key,
java.lang.String defaultValue)
getString in interface android.content.SharedPreferencespublic java.lang.String getUnencryptedString(java.lang.String key,
java.lang.String defaultValue)
key - defaultValue - public java.util.Set<java.lang.String> getStringSet(java.lang.String key,
java.util.Set<java.lang.String> defaultValues)
getStringSet in interface android.content.SharedPreferencespublic int getInt(java.lang.String key,
int defaultValue)
getInt in interface android.content.SharedPreferencespublic long getLong(java.lang.String key,
long defaultValue)
getLong in interface android.content.SharedPreferencespublic float getFloat(java.lang.String key,
float defaultValue)
getFloat in interface android.content.SharedPreferencespublic boolean getBoolean(java.lang.String key,
boolean defaultValue)
getBoolean in interface android.content.SharedPreferencespublic boolean contains(java.lang.String key)
contains in interface android.content.SharedPreferencespublic void handlePasswordChange(java.lang.String newPassword,
android.content.Context context)
throws java.security.GeneralSecurityException
newPassword - java.security.GeneralSecurityExceptionpublic SecurePreferences.Editor edit()
edit in interface android.content.SharedPreferencespublic static boolean isLoggingEnabled()
public static void setLoggingEnabled(boolean loggingEnabled)
public void registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)
registerOnSharedPreferenceChangeListener in interface android.content.SharedPreferencespublic void registerOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener,
boolean decryptKeys)
listener - OnSharedPreferenceChangeListenerdecryptKeys - Callbacks receive the "key" parameter decryptedpublic void unregisterOnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener listener)
unregisterOnSharedPreferenceChangeListener in interface android.content.SharedPreferences