Skip to content

Instantly share code, notes, and snippets.

@isaidamier
Created February 19, 2020 18:03
Show Gist options
  • Save isaidamier/232770b28a092fd03a07029f03dcde3d to your computer and use it in GitHub Desktop.
Save isaidamier/232770b28a092fd03a07029f03dcde3d to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
private lateinit var biometricPrompt: BiometricPrompt
private lateinit var promptInfo: BiometricPrompt.PromptInfo
private var readyToEncrypt: Boolean = false
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
...
biometricPrompt = createBiometricPrompt()
promptInfo = createPromptInfo()
// e.g. secretKeyName = "biometric_sample_encryption_key"
secretKeyName = getString(R.string.secret_key_name)
...
findViewById<Button>(R.id.encrypt_button).setOnClickListener { authenticateToEncrypt() }
findViewById<Button>(R.id.decrypt_button).setOnClickListener { authenticateToDecrypt() }
}
...
private fun authenticateToEncrypt() {
readyToEncrypt = true
if (BiometricManager.from(applicationContext).canAuthenticate() == BiometricManager
.BIOMETRIC_SUCCESS) {
val cipher = cryptographyManager.getInitializedCipherForEncryption(secretKeyName)
biometricPrompt.authenticate(promptInfo, BiometricPrompt.CryptoObject(cipher))
}
}
private fun authenticateToDecrypt() {
readyToEncrypt = false
if (BiometricManager.from(applicationContext).canAuthenticate() == BiometricManager
.BIOMETRIC_SUCCESS) {
val cipher = cryptographyManager.getInitializedCipherForDecryption(secretKeyName,initializationVector)
biometricPrompt.authenticate(promptInfo, BiometricPrompt.CryptoObject(cipher))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment