Skip to content

Instantly share code, notes, and snippets.

@nilsmagnus
Created February 11, 2021 11:51
Show Gist options
  • Save nilsmagnus/006d9e7ce3b99913df64aa86f8924fe8 to your computer and use it in GitHub Desktop.
Save nilsmagnus/006d9e7ce3b99913df64aa86f8924fe8 to your computer and use it in GitHub Desktop.
Using encryptedsharedpreferences
android{
//...
defaultConfig {
minSdkVersion 23 // important
targetSdkVersion 30
//...
}
dependencies{
implementation "androidx.security:security-crypto:1.1.0-alpha03"
}
}
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import androidx.security.crypto.EncryptedSharedPreferences;
import androidx.security.crypto.MasterKey;
class Foo{
public void initInstance(BinaryMessenger messenger, Context context) {
MasterKey key = new MasterKey.Builder(context)
.setKeyGenParameterSpec(
new KeyGenParameterSpec
.Builder(MasterKey.DEFAULT_MASTER_KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setKeySize(256).build())
.build();
preferences = EncryptedSharedPreferences.create(context, "SHARED_PREFERENCES_NAME", key, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment