Skip to content

Instantly share code, notes, and snippets.

@AliAzaz
Last active Oct 6, 2021
Embed
What would you like to do?
Master key with advanced specification in Jetpack Security
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