Android指纹识别

上一篇讲了通过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) {
    //验证成功
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android指纹识别开发可以使用Android操作系统提供的Fingerprint API来实现。开发者可以在应用中使用这些API来构建指纹识别功能,包括指纹认证和指纹验证。使用这些API还可以创建自定义指纹认证界面。 ### 回答2: Android指纹识别开发是指在Android应用中集成和利用设备的指纹识别功能。指纹识别作为一种生物识别技术,已经得到广泛应用,可以用于增加安全性和方便性,特别是在支付、解锁和身份验证等方面。 要进行Android指纹识别开发,首先需要了解Android系统对于指纹识别的支持。从Android 6.0(API Level 23)开始,Android提供了一套指纹API,开发者可以使用该API与设备上的指纹传感器进行交互。指纹API提供了用于管理指纹硬件和进行指纹验证的功能。 在开发过程中,首先需要在AndroidManifest.xml文件中声明指纹识别的权限。接下来,可以使用FingerprintManager类来管理和操作指纹传感器。可以通过调用authenticate()方法进行指纹验证,并在验证结果回调中获取验证结果。 除了指纹验证之外,还可以在应用中利用指纹识别功能进行更高级的操作。例如,可以使用FingerprintManager类的hasEnrolledFingerprints()方法来判断设备上是否已经注册了指纹,以此决定是否显示指纹识别相关的功能。 为了提供更好的用户体验,可以使用BiometricPrompt类来构建更友好的指纹识别界面。BiometricPrompt类提供了标准化的指纹识别对话框,可以自动适配设备的指纹传感器,并提供一致的用户界面和交互方式。 总而言之,Android指纹识别开发为应用提供了更高级别的安全性和身份验证方式。开发者可以通过使用指纹API和相关类来集成和利用设备上的指纹识别功能,增加应用的安全性和便利性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值