Android 指纹识别

    从Android6.0开始,Android sdk提供了指纹识别的功能。

    主要代码如下:

public class FingerprintUtils {

    private static final String KEY_NAME = "key";

    KeyguardManager mKeyguardManager;
    FingerprintManager mFingerprintManager;
    KeyStore mKeyStore;
    KeyGenerator mKeyGenerator;
    Cipher mCipher;
    FingerprintManager.CryptoObject cryptoObject;


    public void init(Context context) {
        mKeyguardManager = context.getSystemService(KeyguardManager.class);
        mFingerprintManager = context.getSystemService(FingerprintManager.class);
        mKeyStore = KeyStore.getInstance("AndroidKeyStore");
        mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/"
                + KeyProperties.ENCRYPTION_PADDING_PKCS7);
        cryptoObject = new FingerprintManager.CryptoObject(mCipher);
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    public boolean prepareFinger() {
        return isKeyguardSecure()
                && createKey()
                && initCipher()
                && isHardwareDetected()
                && hasEnrolledFingerprints();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public boolean isKeyguardSecure() {
        return mKeyguardManager.isKeyguardSecure();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public boolean createKey() {
        try {
            mKeyStore.load(null);
            mKeyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
                    KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                    .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                    .setUserAuthenticationRequired(true)
                    .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7).build());
            mKeyGenerator.generateKey();
            return true;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public boolean initCipher() {
        try {
            mKeyStore.load(null);
            SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
            mCipher.init(Cipher.ENCRYPT_MODE, key);
            return true;
        } catch (KeyPermanentlyInvalidatedException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean isHardwareDetected() {
        return mFingerprintManager.isHardwareDetected();
    }

    public boolean hasEnrolledFingerprints() {
        return mFingerprintManager.hasEnrolledFingerprints();
    }

    public void uninit() {
        mKeyguardManager = null;
        mKeyStore = null;
        mKeyGenerator = null;
        mCipher = null;
        cryptoObject = null;
        mFingerprintManager = null;
    }

    public FingerprintManager.CryptoObject getCrypto() {
        return cryptoObject;
    }

    /**
     * 开启指纹识别
     */
    @TargetApi(23)
    public void open(final Context context, final FingerView fingerView) {
        FingerprintManager fingerprintManager = context.getSystemService(FingerprintManager.class);

        if (BaseConstant.versionCodesMarshMallow > Build.VERSION.SDK_INT || !prepareFinger()) {
            return;
        }
        CancellationSignal cancellationSignal = new CancellationSignal();
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        fingerprintManager.authenticate(getCrypto(), cancellationSignal, 0, new FingerprintManager.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode, CharSequence errString) {
                if (errorCode == 7) {
                    Toast.makeText(context, "尝试次数过多,请使用密码", Toast.LENGTH_SHORT);
                }
            }

            @Override
            public void onAuthenticationHelp(int helpCode, CharSequence helpString) {

            }

            @Override
            public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
                Toast.makeText(context, "指纹识别成功", Toast.LENGTH_SHORT);
            }

            @Override
            public void onAuthenticationFailed() {
                Toast.makeText(context, "指纹识别失败", Toast.LENGTH_SHORT);
            }
        }, null);
    }

}

你可能感兴趣的:(Android技术,指纹识别)