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) {
//验证成功
}

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
检查Android是否支持指纹识别以及是否已经录入指纹
原文: 检查Android是否支持指纹识别以及是否已经录入指纹 Android M 开始,系统中加入了指纹相关功能. 主要用到的类为:FingerprintManager 只提供三个方法: 返回值 方法签名 说明 void authenticate(FingerprintManager.CryptoObject crypto, CancellationSignal cancel, int flags, FingerprintManager.AuthenticationCallback callback, Handler handler) 用于指纹验证 boolean hasEnrolledFingerprints() 确定是否至少有一个指纹登记。 boolean isHardwareDetected() 确定指纹硬件是否存在并且功能正常。 那么有了以上方法,就可以很简单的判断手机是否支持指纹,以及是否有指纹录入.以下为代码实现: /** * 检查指纹 * * @param context * @return */ public static rx.Observable<java....
-
下一篇
Android中对sqlite加密--SQLCipher
原文: Android中对sqlite加密--SQLCipher android中有些时候会将一些隐私数据存放在sqlite数据库中,在root过的手机中通过RE就能够轻松的打开并查看数据库所有内容,所以对隐私数据的保护就有两个方法:①将隐私数据自行加密后存入数据库,别人即使打开查看也不知道是什么数据;②将整个数据库进行加密,别人根本就打不开。对于一个数据库中所有数据都需要加密的情况,直接对数据库进行加密是很好的一个解决方案。 SQLCipher提供两个版本,一个收费版,一个免费版。收费和免费的功能没多大差别,只是收费的集成更简单方便~这里用免费版进行举例: SQLCipher For Android 1、下载sqlcipher-for-android-community-v3.2.0.zip 2、在android studio中新建 assets 文件夹,将下载的文件中assets文件夹中的icudt46l.zip文件拷贝到此处: 3、在 mian 目录下新建jniLibs文件夹,并将下载文件中libs文件夹中对应不同平台so文件的文件夹拷贝到此文件夹下; 4、将libs下的sqlc...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- MySQL数据库在高并发下的优化方案
- SpringBoot2配置默认Tomcat设置,开启更多高级功能