背景:
现在随着手机的普及,以及对使用的智能化,越来越多的安全便捷的功能被用在手机上,比如我们常用的安全密码:面部识别、红膜识别、数字密码、指纹等。由于指纹属于接触性密码,安全度高,操作便捷,也不宜泄露和被他人偷窥。
接入流程:
-
判断系统版本是否在6.0之后
-
判断手机是否支持指纹
-
判断手机中是否已经录入相关指纹
-
以上条件全部满足后,基本可以进行指纹验证
首先新增一个权限:
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
第二部:代码区域
FingerprintManager 的获取,因为它是一个服务,所以获取也是通过getservice获取即可。
FingerprintManager manager =(FingerprintManager)getSystemService(Context.FINGERPRINT_SERVICE);
manager.hasEnrolledFingerprints();//是否录入了指纹
manager.isHardwareDetected();//设备是否支持指纹识别
manager.authenticate();调用授权
参数:
1.CryptoObject:
/**
* A wrapper class for the crypto objects supported by FingerprintManager. Currently the
* framework supports {@link Signature}, {@link Cipher} and {@link Mac} objects.
*/
主要用来封装秘钥的入参对象,加密对象,加你有三种:Signature,Cipher,Mac
我们等下会通过Cipher方式进行验证
2.CancellationSignal
:取消验证的一个类,该类也提供一个方法,设置回调接口的。
3.flags:
默认传0即可//optional flags; should be 0
4.AuthenticationCallback
:验证回调提供的方法
//验证失败,errorCode:错误码,errString:错误信息
public void onAuthenticationError(int errorCode, CharSequence errString)
//验证过程一些信息的输出。
public void onAuthenticationHelp(int helpCode, CharSequence helpString)
//验证成功
public void onAuthenticationSucceeded(AuthenticationResult result)
//验证失败
//获取
onAuthenticationAcquired(int acquireInfo)
5.Handler
:用户自动以的回调,如果没有特殊情况可以传null。
1.获取秘钥:
1.1初始化秘钥,我已提前授权过,
private Cipher mCipher;
private static final String DEFAULT_KEY_NAME = "default_key";
private KeyStore keyStore;
// 初始化密钥库
private void initKey() {
//先判断设备是否支持指纹,再判断是否录入指纹,否则会报错。
FingerprintManager manager = (FingerprintManager) getSystemService(Context.FINGERPRINT_SERVICE);
if (manager == null)
showToast("指纹服务为异常");
return;
boolean hasFlag = manager.hasEnrolledFingerprints();//是否录入了指纹
boolean hardware=manager.isHardwareDetected();//设备是否支持指纹识别
if (!hardware)
showToast("该设备不支持指纹服务");
if (!hasFlag)
showToast("该设备没有录入指纹");
return;
try {
keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(DEFAULT_KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT |
KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
keyGenerator.init(builder.build());
keyGenerator.generateKey();
} catch (Exception e) {
throw new RuntimeException(e);
initCipher();
// 初始化密钥
private void initCipher() {
try {
SecretKey key = (SecretKey) keyStore.getKey(DEFAULT_KEY_NAME, null);
mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
+ KeyProperties.BLOCK_MODE_CBC + "/"
+ KeyProperties.ENCRYPTION_PADDING_PKCS7);
mCipher.init(Cipher.ENCRYPT_MODE, key);
} catch (Exception e) {
throw new RuntimeException(e);
}
1.2处理指纹管理类:
private void initFingerPrint() {
FingerprintManager manager = (FingerprintManager) getSystemService(Context.FINGERPRINT_SERVICE);
if (manager == null)
showToast("指纹服务为异常");
return;
boolean hasFlag = manager.hasEnrolledFingerprints();//是否录入了指纹
boolean hardware=manager.isHardwareDetected();//设备是否支持指纹识别
if (!hardware)
showToast("该设备不支持指纹服务");
if (!hasFlag)
showToast("该设备没有录入指纹");
return;
}
og.e("hasEnrolledFingerprints="+hasFlag);
Log.e("isHardwareDetected="+hardware);
mCancellationSignal=new CancellationSignal();
mCancellationSignal.setOnCancelListener(new CancellationSignal.OnCancelListener() {
@Override
public void onCancel() {
showToast("取消授权");
Log.e("授权:onCancel");
manager.authenticate(getCryptoObect(), mCancellationSignal, 0, new FingerprintManager.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
showToast("onAuthenticationError="+errorCode+","+errString);
Log.e("授权:onAuthenticationError"+errorCode+ errString);
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
super.onAuthenticationHelp(helpCode, helpString);
Log.e("授权:onAuthenticationHelp"+helpCode+ helpString);
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Log.e("授权:onAuthenticationSucceeded"+result.getCryptoObject().getCipher().getAlgorithm());
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Log.e("授权:onAuthenticationFailed");
},new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.e("授权:Message"+msg.what+","+msg.toString());
private FingerprintManager.CryptoObject getCryptoObect()
return new FingerprintManager.CryptoObject(mCipher);
}
秘钥在指纹识别之前获取,然后使用即可。本文档中针对一些代码并没有进行判空处理,如需详细的代码,可在该代码上进行一些判空和权限的检查优化,以及和业务的相结合使用。以上代码经本人测试,验证没问题。