scottyab / secure-preferences

Android Shared preference wrapper than encrypts the values of Shared Preferences. It's not bullet proof security but rather a quick win for incrementally making your android app more secure.
1.53k stars 235 forks source link

SecuredPreference returns null in application class. #82

Open tarunattri opened 6 years ago

tarunattri commented 6 years ago

In my use case I have to check for a String stored in SecuredPreference in Application class onCreate() method.

For that I try to get the String from SecuredPreference in onCreate of MyApplication class. but here most of the times, SecuredPreference returns null values.

If i add same code in my activity, it returns non null value.

Looks like SecuredPreference is taking time to intialize. Any hints on why this is happening. Let me know in case additional info is needed for this.

holysheep commented 6 years ago

Did you use common preference filename for your shared preferences? Which context passed in SecurePreferences constructor?

Problem somehow can be caused by decryption itself, try to breakpoint encryptedValue in SecurePreferences to check it's presence:

  @Override
    public String getString(String key, String defaultValue) {
        final String encryptedValue = sharedPreferences.getString(
                SecurePreferences.hashPrefKey(key), null);

        String decryptedValue = decrypt(encryptedValue);
        if (encryptedValue != null && decryptedValue != null) {
            return decryptedValue;
        } else {
            return defaultValue;
        }
    }
tarunattri commented 6 years ago

PFB my answers inline : Did you use common preference filename for your shared preferences? : Yes I used common preference File name. Which context passed in SecurePreferences constructor? IN Application class's onCreate(), context of application is passed. In case of LauncherActivity class, context of Launcher Activity is passed.

Problem somehow can be caused by decryption itself : If it was problem caused by decryption, than it would exist everywhere, but in my case only in Application class's onCreate it is returning null.

I hope it clears the issue.