· 7 years ago · Oct 17, 2018, 11:36 AM
1package ir.faradroid.fingerprint;
2
3import android.Manifest;
4import android.annotation.TargetApi;
5import android.app.Activity;
6import android.app.KeyguardManager;
7import android.content.Context;
8import android.content.pm.PackageManager;
9import android.hardware.fingerprint.FingerprintManager;
10import android.os.Build;
11import android.os.CancellationSignal;
12import android.security.keystore.KeyGenParameterSpec;
13import android.security.keystore.KeyPermanentlyInvalidatedException;
14import android.security.keystore.KeyProperties;
15import android.support.annotation.NonNull;
16import android.support.v4.app.ActivityCompat;
17import android.support.v7.app.AppCompatActivity;
18import android.widget.Toast;
19
20import java.io.IOException;
21import java.security.InvalidAlgorithmParameterException;
22import java.security.InvalidKeyException;
23import java.security.KeyStore;
24import java.security.KeyStoreException;
25import java.security.NoSuchAlgorithmException;
26import java.security.NoSuchProviderException;
27import java.security.UnrecoverableKeyException;
28import java.security.cert.CertificateException;
29
30import javax.crypto.Cipher;
31import javax.crypto.KeyGenerator;
32import javax.crypto.NoSuchPaddingException;
33import javax.crypto.SecretKey;
34
35public class FingerPrintChecker extends AppCompatActivity {
36
37 private Context context;
38 private GetResponse getResponse;
39
40
41 public final int REQ_PER_FINGERPRINT = 100;
42 KeyGenerator keyGenerator;
43 KeyStore keyStore;
44 private Cipher cipher;
45
46 public final String KEY_NAME = "key_generator";
47
48
49 public FingerPrintChecker(final Context context, final GetResponse getResponse) {
50 this.context = context;
51 this.getResponse = getResponse;
52
53
54 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
55
56
57 if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) !=
58 PackageManager.PERMISSION_GRANTED) {
59 ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.USE_FINGERPRINT}, REQ_PER_FINGERPRINT);
60 }
61
62 }
63 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
64
65 KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(context.KEYGUARD_SERVICE);
66 FingerprintManager fingerprintManager = (FingerprintManager) context.getSystemService(context.FINGERPRINT_SERVICE);
67
68
69 // Check whether the device has a Fingerprint sensor.
70 if (!fingerprintManager.isHardwareDetected()) {
71
72 getResponse.GetCheckFingerPrintResult("Error","Not Supported",false);
73 } else {
74 // Checks whether fingerprint permission is set on manifest
75 if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
76 getResponse.GetCheckFingerPrintResult("Error","Not Permission",false);
77 } else {
78 // Check whether at least one fingerprint is registered
79
80 if (!fingerprintManager.hasEnrolledFingerprints()) {
81 getResponse.GetCheckFingerPrintResult("Error","Is Not Registered",false);
82 } else {
83 // Checks whether lock screen security is enabled or not
84 if (!keyguardManager.isKeyguardSecure()) {
85 getResponse.GetCheckFingerPrintResult("Error","Is Not Enabled",false);
86 } else {
87 generateKey();
88
89
90 if (cipherInit()) {
91 FingerprintManager.CryptoObject cryptoObject = null;
92 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
93 cryptoObject = new FingerprintManager.CryptoObject(cipher);
94 }
95 FingerprintHandler helper = new FingerprintHandler(context, new GetResponse() {
96 @Override
97 public void GetCheckFingerPrintResult(String result, String fullResultText, Boolean status) {
98
99
100 getResponse.GetCheckFingerPrintResult(result,fullResultText,status);
101 }
102 });
103 helper.startAuth(fingerprintManager, cryptoObject);
104 }
105 }
106 }
107 }
108 }
109 }
110 }
111
112
113 @TargetApi(Build.VERSION_CODES.M)
114 protected void generateKey() {
115 try {
116 keyStore = KeyStore.getInstance("AndroidKeyStore");
117 } catch (Exception e) {
118 e.printStackTrace();
119 }
120
121
122 KeyGenerator keyGenerator;
123 try {
124 keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
125 } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
126 throw new RuntimeException("Failed to get KeyGenerator instance", e);
127 }
128
129
130 try {
131 keyStore.load(null);
132 keyGenerator.init(new
133 KeyGenParameterSpec.Builder(KEY_NAME,
134 KeyProperties.PURPOSE_ENCRYPT |
135 KeyProperties.PURPOSE_DECRYPT)
136 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
137 .setUserAuthenticationRequired(true)
138 .setEncryptionPaddings(
139 KeyProperties.ENCRYPTION_PADDING_PKCS7)
140 .build());
141 keyGenerator.generateKey();
142 } catch (NoSuchAlgorithmException |
143 InvalidAlgorithmParameterException
144 | CertificateException | IOException e) {
145 throw new RuntimeException(e);
146 }
147 }
148
149
150 @TargetApi(Build.VERSION_CODES.M)
151 public boolean cipherInit() {
152 try {
153 cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
154 } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
155 throw new RuntimeException("Failed to get Cipher", e);
156 }
157
158
159 try {
160 keyStore.load(null);
161 SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
162 null);
163 cipher.init(Cipher.ENCRYPT_MODE, key);
164 return true;
165 } catch (KeyPermanentlyInvalidatedException e) {
166 return false;
167 } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
168 throw new RuntimeException("Failed to init Cipher", e);
169 }
170 }
171
172
173 @TargetApi(Build.VERSION_CODES.M)
174 public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {
175
176
177 private Context context;
178 private GetResponse getResponse;
179
180
181 // Constructor
182 public FingerprintHandler(Context mContext,GetResponse getResponse) {
183 context = mContext;
184 this.getResponse = getResponse;
185 }
186
187
188 public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {
189 CancellationSignal cancellationSignal = new CancellationSignal();
190 if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
191 return;
192 }
193 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
194 manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
195 }
196 }
197
198
199 @Override
200 public void onAuthenticationError(int errMsgId, CharSequence errString) {
201 getResponse.GetCheckFingerPrintResult("Error","Fingerprint Authentication error\n" + errString, false);
202 }
203
204
205 @Override
206 public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
207
208 getResponse.GetCheckFingerPrintResult("Help","Fingerprint Authentication help\n" + helpString, false);
209
210 }
211
212
213 @Override
214 public void onAuthenticationFailed() {
215
216 getResponse.GetCheckFingerPrintResult("Failed","Fingerprint Authentication failed.", false);
217 }
218
219
220 @Override
221 public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
222 getResponse.GetCheckFingerPrintResult("Success","Fingerprint Authentication succeeded.", true);
223 }
224
225
226 public void update(String e, Boolean success) {
227
228
229 }
230
231 }
232
233 public interface GetResponse {
234 void GetCheckFingerPrintResult(String result,String fullResultText,Boolean status);
235 }
236
237 @Override
238 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
239
240 if (requestCode == REQ_PER_FINGERPRINT) {
241 if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
242
243
244
245 }
246 }
247 }
248
249
250}