Small refactoring to focus the encrypted storage procedures to the application context.

This commit is contained in:
Vitor Pamplona
2024-02-12 11:52:18 -05:00
parent bdff8c6c0d
commit c0a9ce8e21
3 changed files with 30 additions and 22 deletions
@@ -26,6 +26,7 @@ import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy import android.os.StrictMode.ThreadPolicy
import android.os.StrictMode.VmPolicy import android.os.StrictMode.VmPolicy
import android.util.Log import android.util.Log
import androidx.security.crypto.EncryptedSharedPreferences
import coil.ImageLoader import coil.ImageLoader
import coil.disk.DiskCache import coil.disk.DiskCache
import com.vitorpamplona.amethyst.service.playback.VideoCache import com.vitorpamplona.amethyst.service.playback.VideoCache
@@ -87,6 +88,10 @@ class Amethyst : Application() {
return ImageLoader.Builder(applicationContext).diskCache { imageCache } return ImageLoader.Builder(applicationContext).diskCache { imageCache }
} }
fun encryptedStorage(npub: String? = null): EncryptedSharedPreferences {
return EncryptedStorage.preferences(instance, npub)
}
companion object { companion object {
lateinit var instance: Amethyst lateinit var instance: Amethyst
private set private set
@@ -20,32 +20,35 @@
*/ */
package com.vitorpamplona.amethyst package com.vitorpamplona.amethyst
import android.content.Context
import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey import androidx.security.crypto.MasterKey
object EncryptedStorage { class EncryptedStorage {
private const val PREFERENCES_NAME = "secret_keeper" companion object {
private const val PREFERENCES_NAME = "secret_keeper"
// returns the preferences for each account or a global file if null. // returns the preferences for each account or a global file if null.
fun prefsFileName(npub: String? = null): String { fun prefsFileName(npub: String? = null): String {
return if (npub == null) PREFERENCES_NAME else "${PREFERENCES_NAME}_$npub" return if (npub == null) PREFERENCES_NAME else "${PREFERENCES_NAME}_$npub"
} }
fun preferences(npub: String? = null): EncryptedSharedPreferences { fun preferences(
val context = Amethyst.instance applicationContext: Context,
val masterKey: MasterKey = npub: String? = null,
MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS) ): EncryptedSharedPreferences {
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM) val masterKey: MasterKey =
.build() MasterKey.Builder(applicationContext, MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()
val preferencesName = prefsFileName(npub) return EncryptedSharedPreferences.create(
applicationContext,
return EncryptedSharedPreferences.create( prefsFileName(npub),
context, masterKey,
preferencesName, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
masterKey, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, ) as EncryptedSharedPreferences
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM, }
) as EncryptedSharedPreferences
} }
} }
@@ -233,7 +233,7 @@ object LocalPreferences {
if (npub == null) DEBUG_PREFERENCES_NAME else "${DEBUG_PREFERENCES_NAME}_$npub" if (npub == null) DEBUG_PREFERENCES_NAME else "${DEBUG_PREFERENCES_NAME}_$npub"
Amethyst.instance.getSharedPreferences(preferenceFile, Context.MODE_PRIVATE) Amethyst.instance.getSharedPreferences(preferenceFile, Context.MODE_PRIVATE)
} else { } else {
return EncryptedStorage.preferences(npub) return Amethyst.instance.encryptedStorage(npub)
} }
} }