Last active
October 6, 2021 22:36
-
-
Save AliAzaz/11c1f1a6a1499cf41627366b42c42703 to your computer and use it in GitHub Desktop.
Master key with advanced specification in Jetpack Security
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun generateAdvMasterKey(context: Context): MasterKey { | |
val advSpec = KeyGenParameterSpec.Builder( | |
"key_name", // define your master key name | |
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT | |
).apply { | |
setBlockModes(KeyProperties.BLOCK_MODE_GCM) | |
setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE) | |
setKeySize(256) | |
setUserAuthenticationRequired(true) | |
setUserAuthenticationValidityDurationSeconds(120) | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | |
setUnlockDeviceRequired(true) | |
setIsStrongBoxBacked(true) | |
} | |
}.build() | |
val master = MasterKey.Builder(context).setKeyGenParameterSpec(advSpec) | |
return master.build() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment