Skip to content
Snippets Groups Projects

feat/BackgroundServiceAndNetworkSensing (network sensing is implemented using...

Merged zaydanA requested to merge feat/backgroundServiceAndNetworkSensing into main
Compare and
4 files
+ 46
12
Preferences
Compare changes
Files
4
package com.example.pbd_jwr.encryptedSharedPref
package com.example.pbd_jwr.encryptedSharedPref
class EncryptedSharedPref {
import android.content.Context
 
import android.content.SharedPreferences
 
import android.security.keystore.KeyGenParameterSpec
 
import android.security.keystore.KeyProperties
 
import androidx.security.crypto.EncryptedSharedPreferences
 
import androidx.security.crypto.MasterKey
 
 
object EncryptedSharedPref {
 
 
fun create(context: Context, sharedPrefName: String): SharedPreferences {
 
// Create or retrieve the master key for encryption
 
val keyGenParameterSpec = KeyGenParameterSpec.Builder(
 
sharedPrefName,
 
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
 
).setBlockModes(KeyProperties.BLOCK_MODE_GCM)
 
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
 
.setKeySize(256)
 
.build()
 
 
val masterKeyAlias = MasterKey.Builder(context, "login")
 
.setKeyGenParameterSpec(keyGenParameterSpec)
 
.build()
 
 
val sharedPreferences = EncryptedSharedPreferences.create(
 
context,
 
sharedPrefName,
 
masterKeyAlias,
 
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
 
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
 
)
 
 
 
// Create EncryptedSharedPreferences instance
 
return EncryptedSharedPreferences.create(
 
context,
 
sharedPrefName, // Name of the shared preferences file
 
masterKeyAlias,
 
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
 
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
 
)
 
}
 
}
}
 
\ No newline at end of file