You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * [SignatureHelper] provides utility methods to generate and manage key pairs in the Android KeyStore. * This class supports generating hardware-backed key pairs, signing data, verifying signatures, and managing KeyStore entries. * * @param alias The alias of the key entry in the KeyStore. * @param requireBiometricAuth Indicates if strong biometric authentication is required for accessing the key. * @param keyAlgorithm The algorithm to be used for key generation. Default is EC (Elliptic Curve). * @param signatureAlgorithm The algorithm to be used for signing data. Default is "SHA256withECDSA". * @param keyPairProvider The provider for the KeyStore. Default is "AndroidKeyStore".*/classSignatureHelper(
valalias:String,
valrequireBiometricAuth:Boolean = false,
valkeyAlgorithm:String = KeyProperties.KEY_ALGORITHM_EC,
valsignatureAlgorithm:String = "SHA256withECDSA",
valkeyPairProvider:String = "AndroidKeyStore",
) {
/** * Generates and returns key pair and if the pair is inside secure hardware or returns null and * removes the entry if the key pair isn't hardware backed or any error is occurred.*/fungenerateHardwareBackedKeyPair(): KeyPair? {
// Method implementation
}
/** * Generates a key pair. * @return Null if any error is occurred, otherwise the key pair.*/fungenerateKeyPair(): KeyPair? {
// Method implementation
}
/** * Checks if a key entry with the specified alias exists in the KeyStore.*/funexists(): Boolean? {
// Method implementation
}
/** * Deletes the key entry with the specified alias from the KeyStore.*/fundeleteKeyStoreEntry(): Boolean {
// Method implementation
}
/** * Signs the given data using the private key associated with the specified alias, returns null * if an error occurs. * If biometric authentication is required, it must be performed before signing the data.*/funsignData(data:String): SignedData? {
// Method implementation
}
/** * Encodes the public key of the given key pair to a Base64 string.*/fungetPublicKeyBase64Encoded(keyPair:KeyPair): String {
// Method implementation
}
/** * Verifies the given signature using the provided public key and data.*/funverifyData(publicKey:String, data:String, signature:String): Boolean {
// Method implementation
}
/** * Verifies the given signature using the provided public key and data.*/funverifyData(publicKey:PublicKey, data:String, signature:String): Boolean {
// Method implementation
}
/** * Converts a Base64 encoded public key string to a PublicKey object.*/fungetPublicKeyFromString(publicKey:String): PublicKey? {
// Method implementation
}
// Private methods and other internal logic
}
BiometricKeyPairHandler.kt
/** * BiometricKeyPairHandler manages the creation, deletion, and use of hardware-backed key pairs * with biometric authentication. This class utilizes [SignatureHelper] to interact with the Android KeyStore. * * @param alias The alias of the key entry in the KeyStore.*/classBiometricKeyPairHandler(alias:String) {
privateval signatureHelper =SignatureHelper(
alias = alias,
requireBiometricAuth =true
)
/** * If strong biometric is available then returns * [SignatureHelper.generateHardwareBackedKeyPair] otherwise returns null.*/fungenerateHardwareBackedKeyPair(activity:FragmentActivity): KeyPair? {
if (!BiometricAuthHelper.isStrongBiometricAuthAvailable(activity)) {
returnnull
}
return signatureHelper.generateHardwareBackedKeyPair()
}
fundeleteKeyPair(): Boolean {
return signatureHelper.deleteKeyStoreEntry()
}
fungetPublicKeyBase64Encoded(keyPair:KeyPair): String {
return signatureHelper.getPublicKeyBase64Encoded(keyPair)
}
funverifyData(publicKey:String, data:String, signature:String): Boolean {
return signatureHelper.verifyData(publicKey, data, signature)
}
/** * Authenticates the user via biometric authentication and signs the given data. * Calls the provided callback function with the signed data upon successful authentication.*/funauthenticateAndSignData(
data:String,
activity:FragmentActivity,
onAuthenticationSucceeded: (SignedData?) ->Unit
) {
BiometricAuthHelper.authenticate(activity, onAuthenticationSucceeded = {
onAuthenticationSucceeded(signatureHelper.signData(data))
})
}
funexists(): Boolean? {
return signatureHelper.exists()
}
}