Adding the Fingerprint Scanner in Android application
suggest changeAndroid supports the fingerprint API from Android 6.0 (Marshmallow, API 23).
To use this feature in your app, first add the USE_FINGERPRINT permission in your manifest.
<uses-permission
android:name="android.permission.USE_FINGERPRINT" />
Create an asymmetric key pair in the Android Keystore. With setUserAuthenticationRequired(true), the private key can be used only after the user authenticates, including with a fingerprint.
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_EC, "AndroidKeyStore");
keyPairGenerator.initialize(
new KeyGenParameterSpec.Builder(KEY_NAME,
KeyProperties.PURPOSE_SIGN)
.setDigests(KeyProperties.DIGEST_SHA256)
.setAlgorithmParameterSpec(new ECGenParameterSpec("secp256r1"))
.setUserAuthenticationRequired(true)
.build());
keyPairGenerator.generateKeyPair();
setUserAuthenticationRequired(true) rejects use of the private key until the user has authenticated.
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
PublicKey publicKey =
keyStore.getCertificate(MainActivity.KEY_NAME).getPublicKey();
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
PrivateKey key = (PrivateKey) keyStore.getKey(KEY_NAME, null);
Listen for a fingerprint by calling FingerprintManager.authenticate with a CryptoObject initialized from that key. You can instead fall back to a server-verified password.
Obtain the manager with getSystemService(FingerprintManager.class).
Subclass FingerprintManager.AuthenticationCallback and override:
onAuthenticationError
onAuthenticationHelp
onAuthenticationSucceeded
onAuthenticationFailed
To Start
Start listening by calling authenticate with the crypto object:
fingerprintManager
.authenticate(cryptoObject, mCancellationSignal, 0 , this, null);
Cancel
Stop listening by cancelling the same CancellationSignal passed to authenticate:
mCancellationSignal.cancel();
Once the fingerprint (or password) is verified, the FingerprintManager.AuthenticationCallback#onAuthenticationSucceeded() callback is called.
@Override
public void onAuthenticationSucceeded(AuthenticationResult result) {
}