-
Notifications
You must be signed in to change notification settings - Fork 11
/
ExampleThirdPartyKrates.kt
47 lines (40 loc) · 1.78 KB
/
ExampleThirdPartyKrates.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package hu.autsoft.krateexample.krates
import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import com.frybits.harmony.getHarmonySharedPreferences
import hu.autsoft.krate.Krate
import hu.autsoft.krate.default.withDefault
import hu.autsoft.krate.stringPref
/**
* Encrypted Krate based on the AndroidX EncryptedSharedPreferences class.
*/
class EncryptedKrate(applicationContext: Context) : Krate {
override val sharedPreferences: SharedPreferences
init {
// Check the official docs on how to create EncryptedPreferences instances
// https://developer.android.com/topic/security/data
// https://developer.android.com/reference/kotlin/androidx/security/crypto/EncryptedSharedPreferences
val masterKeyAlias = MasterKey.Builder(applicationContext)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
val sharedPrefsFile = "my_sensitive_data.txt"
sharedPreferences = EncryptedSharedPreferences.create(
applicationContext,
sharedPrefsFile,
masterKeyAlias,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}
val myStringValue: String by stringPref("my_string_value").withDefault("")
}
/**
* Harmony is a multi-process SharedPreferences implementation.
* See its GitHub page for details https://github.com/pablobaxter/Harmony
*/
class HarmonyKrate(context: Context) : Krate {
override val sharedPreferences: SharedPreferences = context.getHarmonySharedPreferences("PREF_NAME")
val myStringValue: String by stringPref("my_string_value").withDefault("")
}