· 7 years ago · Jan 11, 2019, 07:36 AM
1package com.ebookfrenzy.fingerprintdemo;
2
3import com.ebookfrenzy.fingerprintdemo.FingerprintHandler;
4import android.support.v7.app.AppCompatActivity;
5import android.os.Bundle;
6import android.app.KeyguardManager;
7import android.hardware.fingerprint.FingerprintManager;
8import android.widget.Toast;
9import android.Manifest;
10import android.content.pm.PackageManager;
11import android.support.v4.app.ActivityCompat;
12
13import java.security.KeyStore;
14import android.security.keystore.KeyProperties;
15
16import java.security.NoSuchAlgorithmException;
17import java.security.NoSuchProviderException;
18
19import javax.crypto.KeyGenerator;
20import android.security.keystore.KeyGenParameterSpec;
21import java.security.cert.CertificateException;
22import java.security.InvalidAlgorithmParameterException;
23import java.io.IOException;
24import android.security.keystore.KeyPermanentlyInvalidatedException;
25import java.security.InvalidKeyException;
26import java.security.KeyStoreException;
27import java.security.UnrecoverableKeyException;
28import javax.crypto.NoSuchPaddingException;
29import javax.crypto.SecretKey;
30import javax.crypto.Cipher;
31
32public class FingerprintDemoActivity extends AppCompatActivity {
33
34 private static final String KEY_NAME = "example_key";
35 private FingerprintManager fingerprintManager;
36 private KeyguardManager keyguardManager;
37 private KeyStore keyStore;
38 private KeyGenerator keyGenerator;
39 private Cipher cipher;
40 private FingerprintManager.CryptoObject cryptoObject;
41
42 @Override
43 protected void onCreate(Bundle savedInstanceState) {
44 super.onCreate(savedInstanceState);
45 setContentView(R.layout.activity_fingerprint_demo);
46
47 keyguardManager =
48 (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
49 fingerprintManager =
50 (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
51
52 if (!keyguardManager.isKeyguardSecure()) {
53
54 Toast.makeText(this,
55 "Lock screen security not enabled in Settings",
56 Toast.LENGTH_LONG).show();
57 return;
58 }
59
60 if (ActivityCompat.checkSelfPermission(this,
61 Manifest.permission.USE_FINGERPRINT) !=
62 PackageManager.PERMISSION_GRANTED) {
63 Toast.makeText(this,
64 "Fingerprint authentication permission not enabled",
65 Toast.LENGTH_LONG).show();
66
67 return;
68 }
69
70 if (!fingerprintManager.hasEnrolledFingerprints()) {
71
72 // This happens when no fingerprints are registered.
73 Toast.makeText(this,
74 "Register at least one fingerprint in Settings",
75 Toast.LENGTH_LONG).show();
76 return;
77 }
78
79 generateKey();
80
81 if (cipherInit()) {
82 cryptoObject =
83 new FingerprintManager.CryptoObject(cipher);
84 FingerprintHandler helper = new FingerprintHandler(this);
85 helper.startAuth(fingerprintManager, cryptoObject);
86 }
87
88 }
89
90
91 protected void generateKey() {
92 try {
93 keyStore = KeyStore.getInstance("AndroidKeyStore");
94 } catch (Exception e) {
95 e.printStackTrace();
96 }
97
98 try {
99 keyGenerator = KeyGenerator.getInstance(
100 KeyProperties.KEY_ALGORITHM_AES,
101 "AndroidKeyStore");
102 } catch (NoSuchAlgorithmException |
103 NoSuchProviderException e) {
104 throw new RuntimeException(
105 "Failed to get KeyGenerator instance", e);
106 }
107
108 try {
109 keyStore.load(null);
110 keyGenerator.init(new
111 KeyGenParameterSpec.Builder(KEY_NAME,
112 KeyProperties.PURPOSE_ENCRYPT |
113 KeyProperties.PURPOSE_DECRYPT)
114 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
115 .setUserAuthenticationRequired(true)
116 .setEncryptionPaddings(
117 KeyProperties.ENCRYPTION_PADDING_PKCS7)
118 .build());
119 keyGenerator.generateKey();
120 } catch (NoSuchAlgorithmException |
121 InvalidAlgorithmParameterException
122 | CertificateException | IOException e) {
123 throw new RuntimeException(e);
124 }
125
126 }
127
128 public boolean cipherInit() {
129 try {
130 cipher = Cipher.getInstance(
131 KeyProperties.KEY_ALGORITHM_AES + "/"
132 + KeyProperties.BLOCK_MODE_CBC + "/"
133 + KeyProperties.ENCRYPTION_PADDING_PKCS7);
134 } catch (NoSuchAlgorithmException |
135 NoSuchPaddingException e) {
136 throw new RuntimeException("Failed to get Cipher", e);
137 }
138
139 try {
140 keyStore.load(null);
141 SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
142 null);
143 cipher.init(Cipher.ENCRYPT_MODE, key);
144 return true;
145 } catch (KeyPermanentlyInvalidatedException e) {
146 return false;
147 } catch (KeyStoreException | CertificateException
148 | UnrecoverableKeyException | IOException
149 | NoSuchAlgorithmException | InvalidKeyException e) {
150 throw new RuntimeException("Failed to init Cipher", e);
151 }
152 }
153
154}