Skip to content

Commit

Permalink
Refactor BiometricHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanTsisyk committed Nov 30, 2024
1 parent 5889085 commit a061f9a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package io.github.romantsisyk.cryptolib.biometrics


import io.github.romantsisyk.cryptolib.crypto.keymanagement.KeyHelper
import javax.crypto.Cipher
import io.github.romantsisyk.cryptolib.crypto.aes.AESEncryption
import android.content.Context
import android.widget.Toast
import androidx.biometric.BiometricPrompt
import androidx.fragment.app.FragmentActivity
import io.github.romantsisyk.cryptolib.crypto.aes.AESEncryption
import io.github.romantsisyk.cryptolib.crypto.keymanagement.KeyHelper
import javax.crypto.Cipher

class BiometricHelper(private val context: Context) {

/**
* Authenticates the user using biometrics and optionally decrypts the provided encrypted data.
* @param activity The activity where the biometric prompt will be displayed.
* @param encryptedData Data to be decrypted upon successful authentication.
* @param title The title displayed on the biometric prompt.
* @param description The description displayed on the biometric prompt.
* @param onSuccess Callback to handle the decrypted data.
* @param onError Callback to handle errors during authentication or decryption.
* @param onAuthenticationError Callback to handle authentication-specific errors.
*/
fun authenticate(
activity: FragmentActivity,
title: String,
Expand All @@ -35,6 +41,7 @@ class BiometricHelper(private val context: Context) {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
try {
// Decrypt the data using the authenticated cipher
val decryptedData = result.cryptoObject?.cipher?.let {
AESEncryption.decrypt(
encryptedData.toString(Charsets.UTF_8),
Expand All @@ -54,14 +61,11 @@ class BiometricHelper(private val context: Context) {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
onAuthenticationError(errorCode, errString)
onError(Exception("Authentication error [$errorCode]: $errString"))

}

override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Toast.makeText(context, "Authentication failed. Try again.", Toast.LENGTH_SHORT)
.show()
onError(Exception("Authentication failed"))
}
}
)
Expand All @@ -72,14 +76,29 @@ class BiometricHelper(private val context: Context) {
biometricPrompt.authenticate(promptInfo, cryptoObject)
}

/**
* Initializes a Cipher object for decryption.
* @return A Cipher initialized with a secret key.
* @throws IllegalStateException if initialization fails.
*/
private fun getCipher(): Cipher {
return try {
val secretKey = KeyHelper.getKey() // Retrieve the secure key
val cipher = KeyHelper.getCipherInstance() // Get Cipher instance
val secretKey = KeyHelper.getKey() // Retrieve the secure key from KeyHelper
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, secretKey)
cipher
} catch (e: Exception) {
throw IllegalStateException("Failed to initialize Cipher", e)
}
}
}

/**
* Decrypts the provided data using the Cipher.
* @param cipher The Cipher used for decryption.
* @param encryptedData The encrypted data to decrypt.
* @return The decrypted data as a ByteArray.
*/
private fun decryptData(cipher: Cipher, encryptedData: ByteArray): ByteArray {
return cipher.doFinal(encryptedData)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.romantsisyk.cryptolib.crypto.manager

import BiometricHelper
import android.app.Activity
import androidx.fragment.app.FragmentActivity
import io.github.romantsisyk.cryptolib.biometrics.BiometricHelper
import io.github.romantsisyk.cryptolib.crypto.config.CryptoConfig
import io.github.romantsisyk.cryptolib.crypto.aes.AESEncryption
import io.github.romantsisyk.cryptolib.crypto.keymanagement.KeyHelper
Expand Down Expand Up @@ -103,11 +103,11 @@ object CryptoManager {
onAuthenticationError = { errorCode, errString ->
onFailure(
AuthenticationException(
"Authentication error [$errorCode]: $errString"
"Authentication error [$errorCode]: $errString"
)
)
},
onError = {exception -> println("Error: ${exception.message}")}
onError = { exception -> println("Error: ${exception.message}") }
)
} else {
onAuthenticated(secretKey)
Expand Down

0 comments on commit a061f9a

Please sign in to comment.