博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android指纹识别
阅读量:4676 次
发布时间:2019-06-09

本文共 4403 字,大约阅读时间需要 14 分钟。

原文:

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

//获取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);

回调函数:

@Overridepublic void onAuthenticationError(int errMsgId, CharSequence errString) {    //验证出现错误了    //errString为错误的信息}@Overridepublic void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {    showError(helpString);    //验证出现一些问题的系统提示,比如:请按久一点等提示信息.}@Overridepublic void onAuthenticationFailed() {    showError("指纹验证失败");    //在验证失败和出现问题以后,系统会继续执行监听,使用者需要在这里修改相关提示信息}@Overridepublic void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {    //验证成功}
posted on
2018-06-03 23:40 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lonelyxmas/p/9131310.html

你可能感兴趣的文章
C#图解教程 第十二章 数组
查看>>
linux常用命令2
查看>>
laravel 关联模型
查看>>
Http请求头安全策略
查看>>
.NET Core开源快速开发框架Colder发布 (NET Core2.1+AdminLTE版)
查看>>
第三次上机
查看>>
JSP页面中的精确到秒的时间控件
查看>>
C#4.0语言新功能及应用 (1)
查看>>
http协议状态码对照表
查看>>
在线电影功能需求
查看>>
appium 1.6.x版本去除安装Unlock、Setting
查看>>
xmapp中 使用admin的权限打开mysql时出现错误1045
查看>>
Objective-C--Runtime机制
查看>>
古文选读161篇--蔡礼旭老师选
查看>>
jquery easyui grid 表格特殊字符处理
查看>>
Android学习之ViewPager
查看>>
Spring笔记
查看>>
LeetCode Weekly Contest 126
查看>>
8封装的意义和拓展性
查看>>
leetcode15
查看>>