iamMehedi / Secured-Preference-Store

A cryptography library and a SharedPreferences wrapper for Android that encrypts the content with 256 bit AES encryption. The Encryption key is securely stored in device's KeyStore.
562 stars 97 forks source link

[Question] - Sharing among multiple apps #50

Open bobpf opened 5 years ago

bobpf commented 5 years ago

Attempting to use this in a library that is shared between two apps. I have set the sharedUserId to the same ID in both apps (and the library). For the context I create a packageContext using the package name of one of my apps. However, I am not able to store the value in one app and fetch it in the other. Just curios to see if this is something that this AWESOME package is preventing me from doing. Should I be looking to move to a Content Provider instead that can use this package to secure the data at rest? Thanks in advance.

My code

  `val sharedContext = context.createPackageContext("com.one.of.my.apps", Context.MODE_PRIVATE)
    SecuredPreferenceStore.init(
        sharedContext,
        storeName,
        keyPrefix,
        seedKey,
        DefaultRecoveryHandler()
    )
    sharedPreferences = SecuredPreferenceStore.getSharedInstance()`
bobpf commented 5 years ago

So I figured out the issue. Application A had a reference to the shared preference before application B wrote the values. When Application A reads the value it did not exist (caching issue). If I restart Application A it is able to read the value. I tried not caching the instance "sharedPreferences from above" but that did not help. Also tried the whole init process every time but that did not make a difference. Not sure what is happening here, but it must be some sort of loading of the shared preferences file and not watching for changes.

iamMehedi commented 5 years ago

I'd prefer ContentProvider over this SharedPreference approach as that is officially suggested. And I also don't think this library is causing the issue you are experiencing. Are you sure that the reason is not 2 different processes trying to concurrently read/write the same file?

bobpf commented 5 years ago

Really sure it’s not a concurrency issue. That being said I wen down the service route instead. Thanks for the update.