-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptedPreferences.kt
More file actions
40 lines (31 loc) · 1.44 KB
/
EncryptedPreferences.kt
File metadata and controls
40 lines (31 loc) · 1.44 KB
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
package com.example.jetpacklibrariesexamples
import android.content.Context
import android.content.SharedPreferences
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
class EncryptSharedPrefs(context: Context) {
companion object {
private const val SHARED_PREFS_NAME = "secret_shared_prefs"
private const val IS_USER_FIRST_TIME = "isUserFirstTime"
}
private var preferences: SharedPreferences
init {
// Step 1: Create or retrieve the Master Key for encryption/decryption
val masterKeyAlias = MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
// Step 2: Initialize/open an instance of EncryptedSharedPreferences
val sharedPreferences: SharedPreferences = EncryptedSharedPreferences.create(
context,
"${context.packageName}_$SHARED_PREFS_NAME",
masterKeyAlias,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
// use the shared preferences and editor as you normally would
preferences = sharedPreferences
}
var isUserFirstTime: Boolean
get() = preferences.getBoolean(IS_USER_FIRST_TIME, true)
set(value) = preferences.edit().putBoolean(IS_USER_FIRST_TIME, value).apply()
}