/** * Small helper class to manage text/icon around fingerprint authentication UI. */ public class FingerprintUiHelper extends FingerprintManager.AuthenticationCallback { private static final long ERROR_TIMEOUT_MILLIS = 1600; private static final long SUCCESS_DELAY_MILLIS = 1300; private final FingerprintManager mFingerprintManager; private final Callback mCallback; private CancellationSignal mCancellationSignal; private boolean mSelfCancelled; /** * Constructor for {@link FingerprintUiHelper}. */ public FingerprintUiHelper(FingerprintManager fingerprintManager, Callback callback) { mFingerprintManager = fingerprintManager; mCallback = callback; } public boolean isFingerprintAuthAvailable() { // The line below prevents the false positive inspection from Android Studio // noinspection ResourceType return mFingerprintManager.isHardwareDetected() && mFingerprintManager.hasEnrolledFingerprints(); } public void startListening(FingerprintManager.CryptoObject cryptoObject) { if (!isFingerprintAuthAvailable()) { return; } mCancellationSignal = new CancellationSignal(); mSelfCancelled = false; // The line below prevents the false positive inspection from Android Studio // noinspection ResourceType mFingerprintManager.authenticate(cryptoObject, mCancellationSignal, 0 /* flags */, this, null); } public void stopListening() { if (mCancellationSignal != null) { mSelfCancelled = true; mCancellationSignal.cancel(); mCancellationSignal = null; } } @Override public void onAuthenticationError(int errMsgId, CharSequence errString) { Log.d("djh", "onAuthenticationError"); if (!mSelfCancelled) { } } @Override public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) { Log.d("djh", "onAuthenticationHelp"); showError(helpString); } @Override public void onAuthenticationFailed() { Log.d("djh", "onAuthenticationFailed"); } @Override public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) { Log.d("djh", "onAuthenticationSucceeded"); stopListening(); } private void showError(CharSequence error) { } public interface Callback { void onAuthenticated(); void onError(); }
安全对象
public class CryptoObjectHelper { // This can be key name you want. Should be unique for the app. static final String KEY_NAME = "com.easylinks.sandbox.fingerprint_authentication_key"; static final String KEYSTORE_NAME = "AndroidKeyStore"; static final String KEY_ALGORITHM = KeyProperties.KEY_ALGORITHM_AES; static final String BLOCK_MODE = KeyProperties.BLOCK_MODE_CBC; static final String ENCRYPTION_PADDING = KeyProperties.ENCRYPTION_PADDING_PKCS7; static final String TRANSFORMATION = KEY_ALGORITHM + "/" + BLOCK_MODE + "/" + ENCRYPTION_PADDING; final KeyStore _keystore; public CryptoObjectHelper() throws Exception { _keystore = KeyStore.getInstance(KEYSTORE_NAME); _keystore.load(null); } public FingerprintManager.CryptoObject buildCryptoObject() throws Exception { Cipher cipher = createCipher(true); return new FingerprintManager.CryptoObject(cipher); } Cipher createCipher(boolean retry) throws Exception { Key key = GetKey(); Cipher cipher = Cipher.getInstance(TRANSFORMATION); try { cipher.init(Cipher.ENCRYPT_MODE | Cipher.DECRYPT_MODE, key); } catch (KeyPermanentlyInvalidatedException e) { _keystore.deleteEntry(KEY_NAME); if (retry) { createCipher(false); } else { throw new Exception("Could not create the cipher for fingerprint authentication.", e); } } return cipher; } Key GetKey() throws Exception { Key secretKey; if (!_keystore.isKeyEntry(KEY_NAME)) { CreateKey(); } secretKey = _keystore.getKey(KEY_NAME, null); return secretKey; } void CreateKey() throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHM, KEYSTORE_NAME); KeyGenParameterSpec keyGenSpec = new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT).setBlockModes(BLOCK_MODE).setEncryptionPaddings(ENCRYPTION_PADDING).setUserAuthenticationRequired(true).build(); keyGen.init(keyGenSpec); keyGen.generateKey(); } }
调用
if (FingerPrintUtiles.isFingerprintAuthAvailable(MainActivity.this)){ FingerprintManager mFingerprintManager = getSystemService(FingerprintManager.class); FingerprintUiHelper fingerprintUiHelper = new FingerprintUiHelper(mFingerprintManager, callback); try { CryptoObjectHelper cryptoObjectHelper = new CryptoObjectHelper(); fingerprintUiHelper.startListening(cryptoObjectHelper.buildCryptoObject()); } catch (Exception e) { e.printStackTrace(); } }else{ ToastUtil.showToastLong(MainActivity.this,"not fingerprint function!"); }