Skip to content

Instantly share code, notes, and snippets.

View isaidamier's full-sized avatar

Isai Damier isaidamier

View GitHub Profile
dependencies {
implementation 'com.google.android.gms:play-services-nearby:18.0.0'
}
binding.useBiometrics.setOnClickListener {
showBiometricPromptForEncryption()
}
....
private fun showBiometricPromptForEncryption() {
val canAuthenticate = BiometricManager.from(applicationContext).canAuthenticate()
if (canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS) {
val secretKeyName = SECRET_KEY_NAME
cryptographyManager = CryptographyManager()
if (ciphertextWrapper != null) {
// user has already enabled biometrics
} else {
// biometrics has not been enabled
}
@isaidamier
isaidamier / BiometricPrompt.ERROR_USER_CANCELED
Last active April 13, 2020 18:46
BiometricPrompt.ERROR_USER_CANCELED
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Log.d(TAG, "$errorCode :: $errString")
if (errorCode == BiometricPrompt.ERROR_USER_CANCELED) {
handleUserCancelation() // this is your custom function
}
}
class MainActivity : AppCompatActivity() {
private lateinit var textInputView: AppCompatEditText
private lateinit var textOutputView: AppCompatTextView
private lateinit var biometricPrompt: BiometricPrompt
private lateinit var promptInfo: BiometricPrompt.PromptInfo
private var readyToEncrypt: Boolean = false
private lateinit var cryptographyManager: CryptographyManager
private lateinit var secretKeyName: String
private lateinit var ciphertext:ByteArray
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)
...
class MainActivity : AppCompatActivity() {
...
private lateinit var biometricPrompt: BiometricPrompt
private lateinit var promptInfo: BiometricPrompt.PromptInfo
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
...
class MainActivity : AppCompatActivity() {
...
private lateinit var biometricPrompt: BiometricPrompt
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
...
biometricPrompt = createBiometricPrompt()
def biometric_version = '1.0.1'
implementation "androidx.biometric:biometric:$biometric_version"
interface CryptographyManager {
/**
* This method first gets or generates an instance of SecretKey and then initializes the Cipher
* with the key. The secret key uses [ENCRYPT_MODE][Cipher.ENCRYPT_MODE] is used.
*/
fun getInitializedCipherForEncryption(keyName: String): Cipher
/**
* This method first gets or generates an instance of SecretKey and then initializes the Cipher