Home

Awesome

KryptoPrefs Android Arsenal

KryptoPrefs is an open source Kotlin library for handling encrypted SharedPreferences in Android.

Features

Getting started

Add the library to your project:

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.rumboalla.KryptoPrefs:kryptoprefs:0.4.3'
}

Create KryptoPrefs and start using it:

val prefs = KryptoBuilder.pref(context, "MyPrefs", MyPassword, MySalt, ApiTarget)
prefs.putString("Key", "Value")
val v = prefs.getString("Key", "Default")

If you need higher level features, use a KryptoContext:

class Prefs(prefs: KryptoPrefs): KryptoContext(prefs) {
    val stringPref = string("stringPref", "defaultValue")
    val intPref = int("intPref", 42)
    val booleanPref = boolean("booleanPref", false)
}

val prefs = Prefs(KryptoBuilder.hybrid(context, "MyPrefs"))
prefs.stringPref("MyString")            // Write to prefs
val stringPref = prefs.stringPref()     // Read from prefs
prefs.stringPref.put("MyString")        // Alternative write to prefs
val stringPref = prefs.stringPref.get() // Alternative read from prefs

Advanced usage

KryptoPref modes

APIEncryptionHashKey
9AesCbcEncryptionSha512HashPbeKey
10+AesCbcEncryptionSha512HashPbkdf2Key
19+AesGcmEncryptionSha512HashPbkdf2Key
val prefs = KryptoBuilder.pref(context, "MyPrefs", MyPassword, MySalt, ApiTarget)
APIEncryptionHashKey
23+AesGcmEncryptionSha512HashKeystoreKey
val prefs = KryptoBuilder.keystore(context, "MyPrefs")
APIEncryptionHashKey
23+AesGcmEncryptionSha512HashHybridKey
val prefs = KryptoBuilder.hybrid(context, "MyPrefs")
APIEncryptionHashKey
9+NoEncryptionNoHashNoKey
val prefs = KryptoBuilder.nocrypt(context, "MyPrefs")

Custom KryptoPref

You can create your own KryptoPref:

val prefs = KryptoPrefsImpl(context, "MyPrefs", AesCbcEncryption(), Sha512Hash(), Pbkdf2Key(MyPassword, MySalt))

Asynchronous operations

You can do async reads and writes:

stringPref.putAsync("MyString")
stringPref.getAsync { newValue = it }

Using custom types, collections and JSON

Add kryptoprefs-gson to your project:

implementation 'com.github.rumboalla.KryptoPrefs:kryptoprefs-gson:0.4.3'

Or kryptoprefs-moshi:

implementation 'com.github.rumboalla.KryptoPrefs:kryptoprefs-moshi:0.4.3'

Use json type in your KryptoContext:

class Prefs(prefs: KryptoPrefs): KryptoContext(prefs) {
    val jsonPref = json("jsonPref", TestClass())
    val listPref = json("listPref", emptyList<TestClass>())
}

Nullable types

Types can be nullable:

val nullDatePref = date("nullDatePref", null)

Memory backing

To help performance preferences can be memory backed:

val stringPrefBacked = string("stringPrefBacked", "defaultValue", true)

Observing preferences

You can observe preferences in a KryptoContext by using += or addObserver:

prefs.intPref += { newValue = it }
prefs.intPref.addObserver { newValue = it }

License

Copyright © 2019 rumboalla.
Licensed under the MIT license.