This library contains the following:
- [
ObscuredSharedPreferences
] (src/main/java/in/co/ophio/secure/core/ObscuredSharedPreferences.java): An implementation ofSharedPreferences
that can store keys and values in obscured manner. - [
ObscuredPreferencesBuilder
] (src/main/java/in/co/ophio/secure/core/ObscuredPreferencesBuilder.java): Can be used to obtain an instance ofObscuredSharedPreferences
. - [
KeyStoreKeyGenerator
] (src/main/java/in/co/ophio/secure/core/KeyStoreKeyGenerator.java): used to generate keys usingAndroid Keystore System
- Main idea by Mike Burton in an answer posted here.
- It is mandatory to set key using
setSecret()
method otherwiseIllegalAccessException
will be thrown. - Uses
PBEWithMD5AndDES
to encode the keys and/or values. - Overrides methods like
putBoolean(), putFloat(), getBoolean(), getFloat(), contains(),
getAll(),
... to provide an obscured implementation.
[ObscuredPreferencesBuilder
] (library/src/main/java/in/co/ophio/secure/core/ObscuredPreferencesBuilder.java)
- Will return an instance of
ObscuredPreferencesBuilder
if atleast one of theobfuscateKey()
ORobfuscateValue()
is set. - If none of the above methods are set it will return simple
SharedPreferences
implementation. - Example:
SharedPreferences sharedPreferences = new ObscuredPreferencesBuilder()
.setApplication(KeystoreApplication.getAppContext())
.obfuscateValue(true)
.obfuscateKey(true)
.setSharePrefFileName(PREFS_NAME)
.setSecret("some key")
.createSharedPrefs();
- method
loadOrGenerateKeys()
: Subsequent calls of same method in an application returns the same key. UsesAndroid Keystore System
to generate and return key. It throwsIllegalStateException
if some error occurred while trying to generate keys. - method
isHardwareBacked()
: Flag indicating that theSecretKeyWrapper
public/private key is hardware-backed. A software keystore is more vulnerable to offline attacks if the device is compromised. - Example:
String key = KeyGenerator.get(KeystoreApplication.getAppContext(),
KeystoreApplication.getAppContext().getPackageName())
.loadOrGenerateKeys();