· 7 years ago · Sep 13, 2018, 04:32 AM
1public class FingerprintAuthenticationActivity extends AppCompatActivity {
2
3private static final String KEY_NAME = "example_key";
4private FingerprintManager fingerprintManager;
5private KeyguardManager keyguardManager;
6private KeyStore keyStore;
7private KeyGenerator keyGenerator;
8private Cipher cipher;
9private FingerprintManager.CryptoObject cryptoObject;
10private FingerprintHandler fingerprintHandler;
11
12@Override
13protected void onCreate(Bundle savedInstanceState) {
14 super.onCreate(savedInstanceState);
15 setContentView(R.layout.activity_fingerprint_authentication);
16
17 keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
18 fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
19
20 if (!keyguardManager.isKeyguardSecure()) {
21
22 Toast.makeText(this, "Lock screen security not enabled in Settings",
23 Toast.LENGTH_LONG).show();
24 return;
25 }
26
27 if (ActivityCompat.checkSelfPermission(this,
28 Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
29 Toast.makeText(this, "Fingerprint authentication permission not enabled",
30 Toast.LENGTH_LONG).show();
31
32 return;
33 }
34
35 if (!fingerprintManager.hasEnrolledFingerprints()) {
36
37 // This happens when no fingerprints are registered.
38 Toast.makeText(this, "Register at least one fingerprint in Settings",
39 Toast.LENGTH_LONG).show();
40 return;
41 }
42
43 generateKey();
44
45 if (cipherInit()) {
46 cryptoObject = new FingerprintManager.CryptoObject(cipher);
47 FingerprintHandler helper = new FingerprintHandler(this);
48 helper.startAuth(fingerprintManager,cryptoObject);
49 }
50
51}
52
53protected void generateKey() {
54 try {
55 keyStore = KeyStore.getInstance("AndroidKeyStore");
56 } catch (Exception e) {
57 e.printStackTrace();
58 }
59
60 try {
61 keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES,
62 "AndroidKeyStore");
63 } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
64 throw new RuntimeException("Failed to get KeyGenerator instance", e);
65 }
66
67 try {
68 keyStore.load(null);
69 keyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
70 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
71 .setUserAuthenticationRequired(true)
72 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
73 .build());
74 keyGenerator.generateKey();
75 } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException | CertificateException | IOException e) {
76 throw new RuntimeException(e);
77 }
78
79}
80
81public boolean cipherInit() {
82 try {
83 cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/"
84 + KeyProperties.ENCRYPTION_PADDING_PKCS7);
85 } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
86 throw new RuntimeException("Failed to get Cipher", e);
87 }
88
89 try {
90 keyStore.load(null);
91 SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,null);
92 cipher.init(Cipher.ENCRYPT_MODE, key);
93 return true;
94 } catch (KeyPermanentlyInvalidatedException e) {
95 return false;
96 } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
97 | NoSuchAlgorithmException | InvalidKeyException e) {
98 throw new RuntimeException("Failed to init Cipher", e);
99 }
100}
101}
102
103public class FingerprintHandler extends FingerprintManager.AuthenticationCallback {
104
105private CancellationSignal cancellationSignal;
106private Context appContext;
107
108
109public FingerprintHandler(Context context) {
110 appContext = context;}
111
112
113public void startAuth(FingerprintManager manager, FingerprintManager.CryptoObject cryptoObject) {
114
115 cancellationSignal = new CancellationSignal();
116
117 if (ActivityCompat.checkSelfPermission(appContext, Manifest.permission.USE_FINGERPRINT) !=
118 PackageManager.PERMISSION_GRANTED) {
119 return;
120 }
121 manager.authenticate(cryptoObject, cancellationSignal, 0, this, null);
122}
123
124@Override
125public void onAuthenticationError(int errMsgId,
126 CharSequence errString) {
127 Toast.makeText(appContext,
128 "Authentication errorn" + errString,
129 Toast.LENGTH_LONG).show();
130}
131
132@Override
133public void onAuthenticationHelp(int helpMsgId,
134 CharSequence helpString) {
135 Toast.makeText(appContext,
136 "Authentication helpn" + helpString,
137 Toast.LENGTH_LONG).show();
138}
139
140@Override
141public void onAuthenticationFailed() {
142 Toast.makeText(appContext,
143 "Authentication failed.",
144 Toast.LENGTH_LONG).show();
145}
146
147@Override
148public void onAuthenticationSucceeded(
149 FingerprintManager.AuthenticationResult result) {
150
151 Toast.makeText(appContext,
152 "Authentication succeeded.",
153 Toast.LENGTH_LONG).show();
154
155
156}
157
158}
159
160public class EmployeeLoginActivity extends AppCompatActivity {
161
162 @Override
163 protected void onCreate(Bundle savedInstanceState) {
164 super.onCreate(savedInstanceState);
165 setContentView(R.layout.activity_employee_login);
166
167 }
168}
169
170@Override
171public void onAuthenticationSucceeded(
172 FingerprintManager.AuthenticationResult result) {
173
174 Toast.makeText(appContext,
175 "Authentication succeeded.",
176 Toast.LENGTH_LONG).show();
177
178 appContext.startActivity(new Intent(appContext,
179 EmployeeLoginActivity.class));
180}
181
182<activity android:name=".EmployeeLoginActivity"></activity>