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);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值