diff --git a/SecurePrefsModule.kt b/SecurePrefsModule.kt new file mode 100644 index 0000000..6a88469 --- /dev/null +++ b/SecurePrefsModule.kt @@ -0,0 +1,65 @@ + +/** + * Requires dependency: + * //security for secured preferences + * implementation "androidx.security:security-crypto:1.0.0-rc02" + */ + +const val STRING_CONSTANT = "STRING_CONSTANT" +const val INT_CONSTANT = "INT_CONSTANT" +const val BOOLEAN_CONSTANT = "BOOLEAN_CONSTANT" +class SecPrefs ( + context: android.content.Context +){ + + private val masterKeyAlias = androidx.security.crypto.MasterKeys.getOrCreate(androidx.security.crypto.MasterKeys.AES256_GCM_SPEC) + private val prefs = androidx.security.crypto.EncryptedSharedPreferences.create( + "encrypted_shared_prefs", + masterKeyAlias, + context, + androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) + + @Synchronized + fun saveStringInPrefs( + input: String + ) { + prefs.edit() + .putString(STRING_CONSTANT, input) + .apply() + } + + @Synchronized + fun loadStringFromPrefs(): String? { + return prefs.getString(STRING_CONSTANT, null) + } + + @Synchronized + fun saveIntInPrefs( + input: Int + ) { + prefs.edit() + .putInt(INT_CONSTANT, input) + .apply() + } + + @Synchronized + fun loadIntFromPrefs(): Int? { + return prefs.getInt(INT_CONSTANT, 0) + } + + @Synchronized + fun saveBooleanInPrefs( + input: Boolean + ) { + prefs.edit() + .putBoolean(BOOLEAN_CONSTANT, input) + .apply() + } + + @Synchronized + fun loadBooleanFromPrefs(): Boolean? { + return prefs.getBoolean(BOOLEAN_CONSTANT, false) + } +}