Created
February 19, 2020 18:03
-
-
Save isaidamier/232770b28a092fd03a07029f03dcde3d to your computer and use it in GitHub Desktop.
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
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