Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tham.biometric;
- import android.app.KeyguardManager;
- import android.content.pm.PackageManager;
- import android.hardware.fingerprint.FingerprintManager;
- import android.Manifest;
- import android.os.Build;
- import android.os.Bundle;
- import android.security.keystore.KeyGenParameterSpec;
- import android.security.keystore.KeyPermanentlyInvalidatedException;
- import android.security.keystore.KeyProperties;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v4.app.ActivityCompat;
- 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;
- import android.util.Log;
- import org.qtproject.qt5.android.bindings.QtActivity;
- import net.vplay.helper.VPlayActivity;
- import com.yourcompany.wizardEVAP.Fingertest.R;
- public class MainActivity extends VPlayActivity {
- private static final String KEY_NAME = "yourKey";
- private Cipher cipher;
- private KeyStore keyStore;
- private KeyGenerator keyGenerator;
- private TextView textView;
- private FingerprintManager.CryptoObject cryptoObject;
- private FingerprintManager fingerprintManager;
- private KeyguardManager keyguardManager;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- Log.d("message", "on create MainActivity of fingerprint auth");
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
- Log.d("message", "Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN");
- keyguardManager =
- (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
- Log.d("message", "create keyguardManager");
- fingerprintManager =
- (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
- Log.d("message", "create fingerprintManager");
- textView = (TextView) findViewById(R.id.textview);
- Log.d("message", "create textView");
- if (!fingerprintManager.isHardwareDetected()) {
- Log.d("message", "Your device doesn't support fingerprint authenticatio");
- textView.setText("Your device doesn't support fingerprint authentication");
- }else{
- Log.d("message", "Your device support fingerprint authenticatio");
- textView.setText("Your device support fingerprint authentication");
- }
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
- Log.d("message", "Please enable the fingerprint permission");
- textView.setText("Please enable the fingerprint permission");
- }
- if (!fingerprintManager.hasEnrolledFingerprints()) {
- Log.d("message", "No fingerprint configured. Please register at least one fingerprint in your device's Setting");
- textView.setText("No fingerprint configured. Please register at least one fingerprint in your device's Settings");
- }
- if (!keyguardManager.isKeyguardSecure()) {
- Log.d("message", "Please enable lockscreen security in your device's Setting");
- textView.setText("Please enable lockscreen security in your device's Settings");
- } else {
- try {
- generateKey();
- } catch (FingerprintException e) {
- e.printStackTrace();
- }
- if (initCipher()){
- cryptoObject = new FingerprintManager.CryptoObject(cipher);
- Log.d("message", "create cryptoObject");
- FingerprintHandler helper = new FingerprintHandler(this);
- Log.d("message", "create helper");
- helper.startAuth(fingerprintManager, cryptoObject);
- Log.d("message", "start auth end");
- }
- }
- }else{
- Log.d("message", "Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN");
- }
- }
- private void generateKey() throws FingerprintException {
- try {
- Log.d("message", "generate key start");
- keyStore = KeyStore.getInstance("AndroidKeyStore");
- Log.d("message", "get keyStore");
- keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
- Log.d("message", "create keyGenerator");
- keyStore.load(null);
- Log.d("message", "init keystore");
- 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());
- Log.d("message", "generate key start");
- keyGenerator.generateKey();
- Log.d("message", "generate key end");
- } catch (KeyStoreException
- | NoSuchAlgorithmException
- | NoSuchProviderException
- | InvalidAlgorithmParameterException
- | CertificateException
- | IOException exc) {
- exc.printStackTrace();
- throw new FingerprintException(exc);
- }
- }
- public boolean initCipher() {
- try {
- Log.d("message", "init cipher start");
- cipher = Cipher.getInstance(
- KeyProperties.KEY_ALGORITHM_AES + "/"
- + KeyProperties.BLOCK_MODE_CBC + "/"
- + KeyProperties.ENCRYPTION_PADDING_PKCS7);
- Log.d("message", "init cipher end");
- } catch (NoSuchAlgorithmException |
- NoSuchPaddingException e) {
- throw new RuntimeException("Failed to get Cipher", e);
- }
- try {
- keyStore.load(null);
- Log.d("message", "create secret key");
- SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
- null);
- Log.d("message", "init cipher");
- cipher.init(Cipher.ENCRYPT_MODE, key);
- Log.d("message", "init cipher end");
- return true;
- } catch (KeyPermanentlyInvalidatedException e) {
- return false;
- } catch (KeyStoreException | CertificateException
- | UnrecoverableKeyException | IOException
- | NoSuchAlgorithmException | InvalidKeyException e) {
- throw new RuntimeException("Failed to init Cipher", e);
- }
- }
- private class FingerprintException extends Exception {
- public FingerprintException(Exception e) {
- super(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement