美文网首页
Android指纹识别

Android指纹识别

作者: Cursor_fei | 来源:发表于2018-01-24 14:05 被阅读160次

上一篇讲了通过FingerprintManager验证手机是否支持指纹识别,以及是否录入了指纹,这里进行指纹的验证.

//获取FingerprintManager实例
FingerprintManager mFingerprintManager =
                                                (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
//执行验证监听
mFingerprintManager
                .authenticate(cryptoObject, mCancellationSignal, 0, this, null);

参数说明:

cryptoObject//FingerprintManager支持的加密对象的包装类。目前该框架支持Signature,Cipher和Mac对象。
mCancellationSignal//提供取消正在进行的操作的功能。
callback(参数中的this)//指纹识别的回调函数

cryptoObject初始化:

private KeyguardManager mKeyguardManager;
private FingerprintManager mFingerprintManager;
private static final String DIALOG_FRAGMENT_TAG = "myFragment";
private static final String SECRET_MESSAGE = "Very secret message";
public static boolean isAuthenticating = false;
public static final String PARAM_DISMISS_DIALOG = "param_dismiss_dialog";
/**
 * Alias for our key in the Android Key Store
 */
private static final String KEY_NAME = "my_key";
private KeyStore mKeyStore;
private KeyGenerator mKeyGenerator;
private Cipher mCipher;

@TargetApi(Build.VERSION_CODES.M)
private boolean initCipher() {
    try {
        mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
                + KeyProperties.BLOCK_MODE_CBC + "/"
                + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new RuntimeException("Failed to get an instance of Cipher", e);
    }
    try {
        mKeyStore.load(null);
        SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
        mCipher.init(Cipher.ENCRYPT_MODE, key);
        return true;
    } catch (KeyPermanentlyInvalidatedException e) {
        return false;
    } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException
            | NoSuchAlgorithmException | InvalidKeyException e) {
        throw new RuntimeException("Failed to init Cipher", e);
    }
}

/**
 * Creates a symmetric key in the Android Key Store which can only be used after the user has
 * authenticated with fingerprint.
 */
@TargetApi(Build.VERSION_CODES.M)
public void createKey() {
    // The enrolling flow for fingerprint. This is where you ask the user to set up fingerprint
    // for your flow. Use of keys is necessary if you need to know if the set of
    // enrolled fingerprints has changed.
    mKeyStore = null;
    mKeyGenerator = null;
    try {
        mKeyStore = KeyStore.getInstance("AndroidKeyStore");
    } catch (KeyStoreException e) {
        throw new RuntimeException("Failed to get an instance of KeyStore", e);
    }
    try {
        mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        throw new RuntimeException("Failed to get an instance of KeyGenerator", e);
    }
    try {
        mKeyStore.load(null);
        // Set the alias of the entry in Android KeyStore where the key will appear
        // and the constrains (purposes) in the constructor of the Builder
        mKeyGenerator.init(new KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                        KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                // Require the user to authenticate with a fingerprint to authorize every use
                // of the key
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7)
                .build());
        mKeyGenerator.generateKey();
    } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException
            | CertificateException | IOException e) {
        throw new RuntimeException(e);
    }
}
FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(mCipher);

回调函数:

@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
    //验证出现错误了
    //errString为错误的信息
}

@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
    showError(helpString);
    //验证出现一些问题的系统提示,比如:请按久一点等提示信息.
}

@Override
public void onAuthenticationFailed() {
    showError("指纹验证失败");
    //在验证失败和出现问题以后,系统会继续执行监听,使用者需要在这里修改相关提示信息
}

@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
    //验证成功
}

相关文章

  • 指纹识别

    一、 指纹识别接口从Android 6.0开始,Android系统加上了对指纹识别的支持。所有指纹识别的接口都在...

  • 指纹识别-Android

    指纹识别-Android @(Android进阶资料)[Android, 学习, 读书笔记, Markdown]指...

  • Android指纹从入门到"放弃"

    Android指纹从入门到"放弃" 一、背景 Android在23(Android M)上新增了对指纹识别的硬件支...

  • Android三星指纹识别简单使用

    前言:对于指纹识别,大多数开发者可能想到的是API 23以后,Android自带的指纹识别sdk,Google对于...

  • 2018-03-05

    Android指纹密码 使用场景以及方向 指纹解锁,在支持指纹识别的手机中,几乎每个支持指纹识别芯片的手机都支持指...

  • 安卓指纹+密码支付(解锁)仿支付宝Demo

    1.前言 Google从Android6.0(api23)就开始提供标准指纹识别支持,并对外提供指纹识别相关的接口...

  • Android中的指纹识别

    最近项目需要使用到指纹识别的功能,查阅了相关资料后,整理成此文。 指纹识别是在Android 6.0之后新增的功能...

  • # android 指纹识别并检测指纹库是否变更

    android API 23时新增的功能,指纹识别 主要类:FingerpringManager 在API28后,...

  • Android指纹识别

    指纹识别大致应用在几种场景1,系统解锁2,应用锁3,支付认证4,普通的登录认证 指纹识别需要手机硬件支持才能使用。...

  • Android 指纹识别

    指纹识别相关api FingerprintManagaerCompat 指纹识别的核心包装类 初始化方法 用于判断...

网友评论

      本文标题:Android指纹识别

      本文链接:https://www.haomeiwen.com/subject/yckuaxtx.html