Hello guys, In this tutorial I will be implementing simple android app which will do one basic functionality and that is biometric fingerprint verification or authentication. One thing I want to share with you guys and that is some limitation of fingerprint in android studio. First fingerprint authentication is supported above the api 23 that is Marshmallow. Android will not allow you to access the fingerprint data or you can not store fingerprint data into any database. Android keep fingerprint data securely in internal storage. If you are thinking to get fingerprint data and save it in some database so it is not possible, android will not allow you to do that. Now question is how to use android api for fingerprint verification or authentication. There are few conditions in order to achieve fingerprint verification or authentication successfully which are


How to implement biometric fingerprint verification or authentication in android studio
How to implement biometric fingerprint verification or authentication in android studio

1. Your device must have fingerprint sensor

2. You must register at least one finger print in your device

3. You can used fingerprint android api only for api 23 and above api level, you can not used for api lower than 23.

4. It uses fingerprint from android internal storage, you can not update and store new fingerprint through app ( not android system ).



Advantages of Fingerprint verification or authentication 


1. It is quick and easy to use.

2. It is highly secured because no body can access it only authenticated person can verify itself by only his fingerprint.

3. It is highly accurate because every person has its own unique pattern in finger and it can not be changed.

4. Required small storage , quickest and secured technique for transaction.

5. It enhance user experience.



Disadvantages of Fingerprint verification or authentication 


1. Fingerprint authentication require additional hardware for fingerprint support

2. Cannot updated and reset once fingerprint registered


Practical Implementation and Demo


First of all I want to show you the output of android app, below are the some images of android app



fingerprint verification or authentication in android studio
fingerprint verification or authentication in android studio


fingerprint verification or authentication in android studio
fingerprint verification or authentication in android studio


fingerprint verification or authentication in android studio
fingerprint verification or authentication in android studio



Here are few steps to implement fingerprint verification and authentication in android studio.

1. Add fingerprint permission in manifest


<uses-permission android:name="android.permission.USE_FINGERPRINT" />

2.  Create class and named it FingerprintHandler, this will handle fingerprint detection and send callback if fingerprint match or fail. Below is the code.




import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.CancellationSignal;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.widget.TextView;
@RequiresApi(api = Build.VERSION_CODES.M)
public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {
private Context con;
// Constructor public FingerprintHandler(Context mContext) {
con = mContext;
}
public void Fingerprint_Auth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {
CancellationSignal cancel = new CancellationSignal();
if (ActivityCompat.checkSelfPermission(con, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
return;
}
manager.authenticate( cryptoObject, cancel, 0, this, null );
}
private void updateStatus(String e){
TextView t1= (TextView) ((Activity) con).findViewById(R.id.error_status);
t1.setText(e);
}

@Override public void onAuthenticationFailed() {
this.updateStatus("Fingerprint Authentication failed.");
}
@Override public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
((Activity) con).finish();
Intent intent = new Intent(con, Dashboard.class);
con.startActivity(intent);
}

@Override public void onAuthenticationError(int errorid, CharSequence error) {
this.updateStatus("Fingerprint Authentication error\n" + error);
}

@Override public void onAuthenticationHelp(int helpMsgId, CharSequence help) {
this.updateStatus("Fingerprint Authentication help\n" + help);
}


}



3. Create empty activity with name of MainActivity and paste below code in it.



import android.Manifest;
import android.annotation.TargetApi;
import android.app.KeyguardManager;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyPermanentlyInvalidatedException;
import android.security.keystore.KeyProperties;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class MainActivity extends AppCompatActivity {
private KeyStore key;
private static final String KEY_NAME = "CoderVlog";
private Cipher cipher;
private TextView t1;
@RequiresApi(api = Build.VERSION_CODES.M)
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fingerprint);
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
FingerprintManager fingerprint = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
t1 = (TextView) findViewById(R.id.error_status);
if(! fingerprint.isHardwareDetected() ){
t1.setText("No Fingerprint Sensor");
}

else {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
t1.setText("Fingerprint authentication permission not enabled");
}
else{
if ( !fingerprint.hasEnrolledFingerprints() ) {
t1.setText("At least one fingerprint must be Register in Settings");
}
else{
if (!keyguardManager.isKeyguardSecure()) {
t1.setText("Lock screen security is not enabled in Settings");
}
else{
generate_Keys();
if ( Initial_cipher() ) {
FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher);
FingerprintHandler helper = new FingerprintHandler(this);
helper.Fingerprint_Auth(fingerprint, cryptoObject);
}
}
}
}
}
}

@TargetApi(Build.VERSION_CODES.M)
public boolean Initial_cipher() {
try {
cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException("Failed to get Cipher", e);
}
try {
key.load(null);
SecretKey key = (SecretKey) this.key.getKey(KEY_NAME,
null);
cipher.init(Cipher.ENCRYPT_MODE, key);
return true;
} catch (KeyPermanentlyInvalidatedException e) {
return false;
} catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
throw new RuntimeException("Failed to init Cipher", e);
}
}

@TargetApi(Build.VERSION_CODES.M)
protected void generate_Keys() {
try {
key = KeyStore.getInstance("AndroidKeyStore");
} catch (Exception e) {
e.printStackTrace();
}
KeyGenerator keyGenerator;
try {
keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
throw new RuntimeException( e );
}
try {
key.load(null);
keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT |
KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(
KeyProperties.ENCRYPTION_PADDING_PKCS7)
.build());
keyGenerator.generateKey();
} catch (NoSuchAlgorithmException |
InvalidAlgorithmParameterException
| CertificateException | IOException e) {
throw new RuntimeException(e);
}
}

}


4. Design code of MainActivity (xml code)



<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_fingerprint" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/design_default_color_primary">

<ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:layout_marginBottom="51dp" android:paddingTop="2dp" android:src="@drawable/ic_action_fingerprint" app:layout_constraintBottom_toTopOf="@+id/textView2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
<TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="40dp" android:text="Touch to Sign In" android:textColor="@color/textPrimary" android:textSize="24sp" app:layout_constraintBottom_toTopOf="@+id/error_status" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/icon" />

<TextView android:id="@+id/error_status" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginBottom="358dp" android:gravity="center" android:paddingStart="30dp" android:paddingEnd="30dp" android:textAlignment="center" android:textColor="@color/errorText" android:textSize="14sp" app:layout_constraintBottom_toTopOf="@+id/textView3" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView android:id="@+id/textView3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginLeft="16dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:layout_marginBottom="25dp" android:text="Your device must have atleast one fingerprint registered. Add fingerprint in device setting" android:textAlignment="center" android:textColor="@color/textPrimaryDark" android:textSize="14sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/error_status" />
</android.support.constraint.ConstraintLayout>


5. Create empty activity with name of Dashboard and That it. The app is ready.

If you have still problem then I have last solution for you. Download the project from link below.

Download Me 

Thanks see you in next tutorial.


Previous Post Next Post