Skip to content

Instantly share code, notes, and snippets.

@AliAzaz
Last active October 6, 2021 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AliAzaz/11c1f1a6a1499cf41627366b42c42703 to your computer and use it in GitHub Desktop.
Save AliAzaz/11c1f1a6a1499cf41627366b42c42703 to your computer and use it in GitHub Desktop.
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