Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Fingerprint scanner

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 798
    Comment on it

    Hello guys wellcome to the tutorial of fingerprint scanner,

     

    This is a simple tutorial through which you could implement one touch login to your application.

    Following are the steps to implement it.


     

    Step 1. Add permission to the manifest file. Eg.

     

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.amitrai.fingerprintauthentication">
    
    <uses-permission
    android:name="android.permission.USE_FINGERPRINT" />
    .
    .
    .


     

    Step 2 :- Create an activity and create this methods in it.

     

    > generateKey()  It will generate encryption key which will be store securely on device.

    > cipherInit() It initialize the cipher which will be used for creating encrypted FingerPrintManager.


     

    private void generateKey(){
    
    try {
    
    keyStore = KeyStore.getInstance("AndroidKeyStore");
    
    } catch (KeyStoreException e) {
    
    e.printStackTrace();
    
    }
    
    
    
    try {
    
    keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,
    
    "AndroidKeyStore");
    
    } catch (NoSuchAlgorithmException e) {
    
    e.printStackTrace();
    
    } catch (NoSuchProviderException e) {
    
    e.printStackTrace();
    
    }
    
    
    
    try {
    
    keyStore.load(null);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    
    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) {
    
    e.printStackTrace();
    
    }
    
    }
    
    
    
    
    
    @TargetApi(Build.VERSION_CODES.M)
    
    public boolean cipherInit() {
    
    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 {
    
    keyStore.load(null);
    
    SecretKey key = (SecretKey) keyStore.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);
    
    }
    
    }

    Step 3 :-  Create a class that implement FingerprintManager.AuthenticationCallback and override all its methods like.

     

    onAuthenticationError
    
    
    
    onAuthenticationHelp
    
    
    
    onAuthenticationFailed
    
    
    
    onAuthenticationSucceeded
    
    



     

    Step 4 : -  Apply version checks and hardware checks as fingerpring api is only available in android m so make sure your device is running on marshmallow or above. Also check if device has hardware available for same.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //Fingerprint API only available on from Android 6.0 (M)
                FingerprintManager fingerprintManager = (FingerprintManager) activity.getSystemService(Context.FINGERPRINT_SERVICE);
                if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {


     

    Step 5 :-  Call the authenticate method and observe the result on callback.

     

    manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);

     

    That's it you have now implemented one touch login.

     

    You can click this link to see the demo on github.



    Happy Coding :)

     

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: