Giter Club home page Giter Club logo

ksprefs's Issues

Experiencing bugs around pull

The basic stacktrace is as following

com.cioccarellia.ksprefs.exceptions.EngineException: 
 at com.cioccarellia.ksprefs.exceptions.EngineException$Companion.convertFrom(EngineException.kt:34)
 at com.cioccarellia.ksprefs.extensions.ExceptionExtsKt.getOrThrowException(ExceptionExts.kt:23)
 at com.cioccarellia.ksprefs.internal.SafeRun$DefaultImpls.runSafely(SafeRun.kt:26)
 at com.cioccarellia.ksprefs.engines.base.Engine.runSafely(Engine.kt:25)
 at com.cioccarellia.ksprefs.internal.SafeRun$DefaultImpls.runSafely$default(SafeRun.kt:24)
 at com.cioccarellia.ksprefs.engines.model.keystore.AesKeyStoreEngine.decrypt(AesKeyStoreEngine.kt:77)
 at com.cioccarellia.ksprefs.engines.model.keystore.AesKeyStoreEngine.integrate-Wp7FQgo(AesKeyStoreEngine.kt:51)
 at com.cioccarellia.ksprefs.enclosure.KspEnclosure.read$library(KspEnclosure.kt:111)
 at com.cioccarellia.ksprefs.dispatcher.KspDispatcher.pull(KspDispatcher.kt:73)
 at com.cioccarellia.ksprefs.KsPrefs.pull(KsPrefs.kt:152)
 at ---------------.ui.fragment.HomeFragment.onViewCreated(HomeFragment.kt:119)
 at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:332)
 at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1199)
 at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2236)
 at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2009)
 at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1965)
 at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1861)
 at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
 at android.os.Handler.handleCallback(Handler.java:883)
 at android.os.Handler.dispatchMessage(Handler.java:100)
 at android.os.Looper.loop(Looper.java:237)
 at android.app.ActivityThread.main(ActivityThread.java:7948)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by: javax.crypto.IllegalBlockSizeException
 at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:519)
 at javax.crypto.Cipher.doFinal(Cipher.java:2055)
 at com.cioccarellia.ksprefs.engines.model.keystore.AesKeyStoreEngine$decrypt$1.invoke(AesKeyStoreEngine.kt:78)
 at com.cioccarellia.ksprefs.engines.model.keystore.AesKeyStoreEngine$decrypt$1.invoke(AesKeyStoreEngine.kt:34)
 ... 23 more
Caused by: android.security.KeyStoreException: Invalid operation handle
 at android.security.KeyStore.getKeyStoreException(KeyStore.java:1550)
 at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.update(KeyStoreCryptoOperationChunkedStreamer.java:132)
 at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:217)
 at android.security.keystore.AndroidKeyStoreAuthenticatedAESCipherSpi$BufferAllOutputUntilDoFinalStreamer.doFinal(AndroidKeyStoreAuthenticatedAESCipherSpi.java:373)
 at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:506)
 ... 26 more

My configuration at app start is:

val prefs by lazy {
    KsPrefs(appContext = instance) {
        encryptionType = EncryptionType.KeyStore("prefs")
        commitStrategy = CommitStrategy.COMMIT
    }
}

Different initialization classes

The default KsPref constructor accepts a base class which is manually configurable by the user to tune preferences for its service.

A nice idea would be to create different constructors with a different class parameter to infer config data

Create secondary engines

Allow engines to have sub-engines to run microtasks or device-dependent operations, to keep the main engine clear and nice. Maybe a nicer name may come in handy.

Maybe array typo

I'm not sure about that, but I think you do have a typo in this array.
I think it should be "abcdefghijklmnopqrstuvwxyz123456789".

java.lang.NullPointerException

Thanks for providing this library.

When I released new version of my android app which was using this library,
App gets huge crashes because of the following function.

Caused by java.lang.NullPointerException
com.cioccarellia.ksprefs.extensions.ReaderExtsKt.readOrThrow (ReaderExts.kt:34)
com.cioccarellia.ksprefs.enclosure.KspEnclosure.readUnsafe$library (KspEnclosure.kt:69)
com.cioccarellia.ksprefs.dispatcher.KspDispatcher.pull (KspDispatcher.kt:88)

Looking forward to hear from you.
Thanks.

Adding dependency to gradle

Hey, i am getting an error when i try to add the
implementation 'com.cioccarellia.ksprefs:2.0.0-rc1' to the gradle.

It says -> ERROR: Failed to resolve: com.cioccarellia.ksprefs:2.0.0-rc1:
Any idea why this is happening?

java.security.InvalidKeyException: Key for algorithm RSA not suitable for symmetric encryption on API < 23

java.lang.RuntimeException: Unable to create application <censored>: com.cioccarellia.ksprefs.exceptions.EngineException: 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4521)
        at android.app.ActivityThread.access$1500(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1339)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: com.cioccarellia.ksprefs.exceptions.EngineException: 
        at com.cioccarellia.ksprefs.exceptions.EngineException$Companion.convertFrom(EngineException.kt:34)
        at com.cioccarellia.ksprefs.extensions.ExceptionExtsKt.getOrThrowException(ExceptionExts.kt:23)
        at com.cioccarellia.ksprefs.internal.SafeRun$DefaultImpls.runSafely(SafeRun.kt:26)
        at com.cioccarellia.ksprefs.engines.base.Engine.runSafely(Engine.kt:25)
        at com.cioccarellia.ksprefs.internal.SafeRun$DefaultImpls.runSafely$default(SafeRun.kt:24)
        at com.cioccarellia.ksprefs.engines.model.keystore.RsaKeyPairKeyStoreEngine.encrypt(RsaKeyPairKeyStoreEngine.kt:68)
        at com.cioccarellia.ksprefs.engines.model.keystore.RsaKeyPairKeyStoreEngine.derive-qJcYHfg(RsaKeyPairKeyStoreEngine.kt:61)
        at com.cioccarellia.ksprefs.enclosure.KspEnclosure.read$library(KspEnclosure.kt:100)
        at com.cioccarellia.ksprefs.dispatcher.KspDispatcher.pull(KspDispatcher.kt:73)
        at com.cioccarellia.ksprefs.KsPrefs.pull(KsPrefs.kt:152)
        at com.cioccarellia.ksprefs.delegates.dynamic.DelegateDynamicKsPref.getValue(DelegateDynamicKsPref.kt:30)
Caused by: java.security.InvalidKeyException: Key for algorithm RSA not suitable for symmetric enryption.
        at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(BaseBlockCipher.java:464)
        at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(BaseBlockCipher.java:758)
        at javax.crypto.Cipher.init(Cipher.java:661)
        at javax.crypto.Cipher.init(Cipher.java:621)
        at com.cioccarellia.ksprefs.extensions.CipherExtsKt.initEncryptKeyPair(CipherExts.kt:60)
        at com.cioccarellia.ksprefs.engines.model.keystore.RsaKeyPairKeyStoreEngine.getEncryptionCipher(RsaKeyPairKeyStoreEngine.kt:52)
        at com.cioccarellia.ksprefs.engines.model.keystore.RsaKeyPairKeyStoreEngine.access$getEncryptionCipher$p(RsaKeyPairKeyStoreEngine.kt:32)
        at com.cioccarellia.ksprefs.engines.model.keystore.RsaKeyPairKeyStoreEngine$encrypt$1.invoke(RsaKeyPairKeyStoreEngine.kt:70)
        at com.cioccarellia.ksprefs.engines.model.keystore.RsaKeyPairKeyStoreEngine$encrypt$1.invoke(RsaKeyPairKeyStoreEngine.kt:32)

My config:

KsPrefs(context) {
    this.encryptionType = EncryptionType.KeyStore("prefs")
}

I've read through the other issues about using the AndroidKeyStore and how it works differently on API < 23. I'm assuming that the library handles all that internally - Am I misusing it?

Observable property general listener does not use primitive key to access storage

Observed callbacks are not being invoked correctly while using an EncryptionType different than plaintext, because the key is confronted with the key inside the observedPrefs map, and those wto keys have a different grade.

// Works for plaintext, fails for base64, aes and keystore
val observedPref = observedPrefs[key] ?: return@OnSharedPreferenceChangeListener

Integrating the incoming key will match the one inside the map.

[Feature Request] Add support for pullOrNull

Sometimes it would really ease the job if there was a method like pullOrNull(key: String): T?. The only workaround I've found is something like this

    @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
    inline fun <reified T : Any> pullOrNull(key: String): T? = if (prefs.exists(key)) {
        prefs.pull(key)
    } else {
        null
    }

but obviously I would like to avoid this :)

javax.crypto.IllegalBlockSizeException when pulling value consecutively

I'm using ksprefs library and from time to time i get javax.crypto.IllegalBlockSizeException
I've found that if 2 threads or 2 calls are made at the same time the app crashes with the above exception.
I can easily recreate the crash using these lines on samsung galaxy tab:

    for (i in 1..10){
        lifecycleScope.launch(Dispatchers.Default){
            val isEnabled = ZoeyApp.prefs.pull(booleanKey, true)
            Log.d("Testing", isEnabled.toString())
        }
    }

I have the prefs set us as such:

val prefs by lazy { KsPrefs(context) {
   encryptionType = EncryptionType.KeyStore("prefs")
}}

Thank you very much for the great lib!

Exists is broken when encryption is enabled

In KspEnclosure the exists method doesn't use deriveKey which breaks the method in case of encryption is enabled.

This

    internal fun exists(
        key: String
    ) = sharedReader.exists(key)

should be

    internal fun exists(
        key: String
    ) = sharedReader.exists(deriveKey(key))

Tested with 2.2.4 and 2.2.5

Add support for AndroidKeyStore key storage

In your README, you've suggested using cipher.so, but we could also use the AndroidKeyStore to:

  • Store the symmetric key for API >= 23
  • Store an asymmetric keypair to wrap a symmetric key for 18 < API < 23

I'd be happy to submit a pull request for it. This would allow users of this library to get a much enhanced layer of security for free!

Add enum-to-byte conversion

This is a really cool feature as it can be nice for settings screens and configuration-related stuff.
I can implement this in 2 ways.

  • Index-based: If an enum has 3 elements, we store as a binary representation its index.
  • name-based: If an enum has 3 elements, we store as a binary representation its name.

Cipher decryption data is with a wrong padding (BadPaddingException)

Hi, I'm getting this intermittent error while getting accessToken saved in CryptoPrefs. I have no control of the accessToken value saved to the preference as it's coming from the API, do you guys have any idea why it's happening?

Fatal Exception: com.andreacioccarelli.cryptoprefs.exceptions.CryptoPreferencesException: Cipher decryption data is with a wrong padding (BadPaddingException). input = [�K2|E��D��F��0p�XL��e��9� �-���::���A�8�t��O?�OF�u���l]�h]�{5�f�A�t���g�] javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
       at com.andreacioccarelli.cryptoprefs.wrappers.PrefsEncrypter.finalize + 108(PrefsEncrypter.kt:108)
       at com.andreacioccarelli.cryptoprefs.wrappers.PrefsEncrypter.decrypt + 89(PrefsEncrypter.kt:89)
       at com.andreacioccarelli.cryptoprefs.CryptoWrapper.get + 35(CryptoWrapper.kt:35)
       at com.andreacioccarelli.cryptoprefs.CryptoPrefs.get + 46(CryptoPrefs.kt:46)
       at co.sleekr.sleekrhr.core.retrofit.NetworkBuilder$authenticator$1.invokeSuspend + 117(NetworkBuilder.kt:117)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith + 33(ContinuationImpl.kt:33)
       at kotlinx.coroutines.DispatchedTask.run + 233(Dispatched.kt:233)
       at kotlinx.coroutines.EventLoopImplBase.processNextEvent + 116(EventLoop.kt:116)
       at kotlinx.coroutines.BlockingCoroutine.joinBlocking + 76(Builders.kt:76)
       at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking + 53(Builders.kt:53)
       at kotlinx.coroutines.BuildersKt.runBlocking + 1(:1)
       at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default + 35(Builders.kt:35)
       at kotlinx.coroutines.BuildersKt.runBlocking$default + 1(:1)
       at co.sleekr.sleekrhr.core.retrofit.NetworkBuilder.authenticator + 99(NetworkBuilder.kt:99)
       at co.sleekr.sleekrhr.core.retrofit.NetworkBuilder.access$authenticator + 33(NetworkBuilder.kt:33)
       at co.sleekr.sleekrhr.core.retrofit.NetworkBuilder$mySleekrHttpClient$$inlined$let$lambda$1.authenticate + 89(NetworkBuilder.kt:89)
       at okhttp3.internal.http.RetryAndFollowUpInterceptor.followUpRequest + 288(RetryAndFollowUpInterceptor.java:288)
       at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept + 158(RetryAndFollowUpInterceptor.java:158)
       at okhttp3.internal.http.RealInterceptorChain.proceed + 147(RealInterceptorChain.java:147)
       at okhttp3.internal.http.RealInterceptorChain.proceed + 121(RealInterceptorChain.java:121)
       at co.sleekr.sleekrhr.core.retrofit.NetworkBuilder$queryParameter$1.intercept + 173(NetworkBuilder.kt:173)
       at okhttp3.internal.http.RealInterceptorChain.proceed + 147(RealInterceptorChain.java:147)
       at okhttp3.internal.http.RealInterceptorChain.proceed + 121(RealInterceptorChain.java:121)
       at co.sleekr.sleekrhr.core.retrofit.NetworkBuilder$headerInterceptor$1.intercept + 145(NetworkBuilder.kt:145)
       at okhttp3.internal.http.RealInterceptorChain.proceed + 147(RealInterceptorChain.java:147)
       at okhttp3.internal.http.RealInterceptorChain.proceed + 121(RealInterceptorChain.java:121)
       at okhttp3.logging.HttpLoggingInterceptor.intercept + 213(HttpLoggingInterceptor.java:213)
       at okhttp3.internal.http.RealInterceptorChain.proceed + 147(RealInterceptorChain.java:147)
       at okhttp3.internal.http.RealInterceptorChain.proceed + 121(RealInterceptorChain.java:121)
       at co.sleekr.sleekrhr.core.retrofit.NetworkBuilder$checkConnectivityInterceptor$1.intercept + 187(NetworkBuilder.kt:187)
       at okhttp3.internal.http.RealInterceptorChain.proceed + 147(RealInterceptorChain.java:147)
       at okhttp3.internal.http.RealInterceptorChain.proceed + 121(RealInterceptorChain.java:121)
       at okhttp3.RealCall.getResponseWithInterceptorChain + 200(RealCall.java:200)
       at okhttp3.RealCall$AsyncCall.execute + 147(RealCall.java:147)
       at okhttp3.internal.NamedRunnable.run + 32(NamedRunnable.java:32)
       at java.util.concurrent.ThreadPoolExecutor.runWorker + 1162(ThreadPoolExecutor.java:1162)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run + 636(ThreadPoolExecutor.java:636)
       at java.lang.Thread.run + 764(Thread.java:764)

here's how i try to get the accessToken:
sharedPref.pref().get(REFRESH_TOKEN, emptyString())

pref() returns CryptoPref

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.