Skip to content

Instantly share code, notes, and snippets.

@mage1k99
Created May 26, 2019 12:27
Show Gist options
  • Save mage1k99/f90cf5535439b733713f8439d2b4b2ac to your computer and use it in GitHub Desktop.
Save mage1k99/f90cf5535439b733713f8439d2b4b2ac to your computer and use it in GitHub Desktop.
The Full Code for fingerprint
package xo.chilout.justtry;
import android.annotation.TargetApi;
import android.app.KeyguardManager;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.support.annotation.RequiresApi;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;
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;
import xo.chilout.justtry.databinding.ActivityFirstthirdBinding;
import xo.chilout.justtry.fingerprint.FingerprintHandler;
public class firstthird extends AppCompatActivity {
ActivityFirstthirdBinding thirdBind;
private KeyStore keyStoreObject;
private static final String key_name = "MyOwnKeyName";
protected static final String TAG = "FirstThird Activity";
private Cipher cipherObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thirdBind = DataBindingUtil.setContentView(this, R.layout.activity_firstthird);
KeyguardManager keyguardManagerObject = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
//FingerprintManagerCompat fingerprintobject = (FingerprintManagerCompat) getSystemService(FINGERPRINT_SERVICE);
FingerprintManagerCompat fingerprintmanger = FingerprintManagerCompat.from(this);
Log.d(TAG, "onCreate: Successfully Done with fingerprintManagerCompact");
checkfingerprint(keyguardManagerObject, fingerprintmanger);
}
@TargetApi(Build.VERSION_CODES.M)
private void checkfingerprint(KeyguardManager keyguardManagerobj1, FingerprintManagerCompat fingerprintManagerCompatobj2) {
if (!fingerprintManagerCompatobj2.isHardwareDetected()) {
Toast.makeText(this, "Fingerprint Not Available", Toast.LENGTH_SHORT).show();
} else {
if (!fingerprintManagerCompatobj2.hasEnrolledFingerprints()) {
Toast.makeText(this, "Register least One Fingerprint", Toast.LENGTH_SHORT).show();
} else if (!keyguardManagerobj1.isDeviceSecure()) {
Toast.makeText(this, "lock screen not enabled", Toast.LENGTH_SHORT).show();
} else {
generateKeys();
if (cipherinitalize()) {
FingerprintManagerCompat.CryptoObject cryptoObject = new FingerprintManagerCompat.CryptoObject(cipherObject);
FingerprintHandler fingerprintHandlerObject = new FingerprintHandler(this);
fingerprintHandlerObject.startauthendication(fingerprintManagerCompatobj2, cryptoObject);
}
}
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private void generateKeys() {
try {
keyStoreObject = KeyStore.getInstance("AndroidKeystore");
} catch (KeyStoreException e) {
e.printStackTrace();
}
KeyGenerator keyGeneratorObject;
try {
keyGeneratorObject = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,"AndroidKeystore");
keyStoreObject.load(null);
keyGeneratorObject.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());
} catch (NoSuchAlgorithmException | CertificateException | IOException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private boolean cipherinitalize() {
try {
cipherObject = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
keyStoreObject.load(null);
SecretKey secretKeyObject = (SecretKey) keyStoreObject.getKey(key_name,null);
cipherObject.init(Cipher.ENCRYPT_MODE, secretKeyObject);
return true;
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "cipherinitalize: IOException error Occared", e);
return false;
} catch (CertificateException e) {
e.printStackTrace();
Log.e(TAG, "cipherinitalize: Certificate Exception",e );
return false;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
Log.e(TAG, "cipherinitalize: NoSuchAlgorithm exist", e);
return false;
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
Log.e(TAG, "cipherinitalize: UnrecoverableKey Exception", e);
return false;
} catch (InvalidKeyException e) {
e.printStackTrace();
Log.e(TAG, "cipherinitalize: InvalidKeyException",e);
return false;
} catch (NoSuchPaddingException e) {
e.printStackTrace();
Log.e(TAG, "cipherinitalize: NoSuchPaddingException",e );
return false;
} catch (KeyStoreException e) {
e.printStackTrace();
Log.e(TAG, "cipherinitalize: Keystore Exception", e);
return false;
}
}
}
package xo.chilout.justtry.fingerprint;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.hardware.fingerprint.FingerprintManagerCompat;
import android.support.v4.os.CancellationSignal;
import android.widget.Toast;
import xo.chilout.justtry.secondone;
//A new Class Cancellation Signal
// Created by Mag on 5/26/2019.
public class FingerprintHandler extends FingerprintManagerCompat.AuthenticationCallback {
private Context contextObject;
public FingerprintHandler(Context context) {
this.contextObject = context;
}
@SuppressWarnings("SpellCheckingInspection")
public void startauthendication(FingerprintManagerCompat fingerprintManagerCompatObject, FingerprintManagerCompat.CryptoObject cryptoObject) {
/*CancellationSignal cancellationSignalObject = new CancellationSignal(); --> this is not used here, Same As CancellationSignal
but not in android.support.v4.compat*/
// @param cancellationObject must be of type CancellationSignal of android.support.v4.os As we use FingerprintManagerCompat
android.support.v4.os.CancellationSignal cancellationSignalObject = new CancellationSignal();
//noinspection deprecation
if(ActivityCompat.checkSelfPermission(contextObject, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED)
return ;
fingerprintManagerCompatObject.authenticate(cryptoObject,0,cancellationSignalObject,this,null);
}
@Override
public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(contextObject,"authentication Successful",Toast.LENGTH_SHORT).show();
contextObject.startActivity(new Intent(contextObject, secondone.class));
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Toast.makeText(contextObject,"Failed to authenticate Fingerprint",Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment