写点什么

安卓指纹对称加密及登录功能的实现 (1),技术实现

用户头像
Android架构
关注
发布于: 刚刚

cipher.init(Cipher.ENCRYPT_MODE | Cipher.DECRYPT_MODE, key);} catch (KeyPermanentlyInvalidatedException e) {_keystore.deleteEntry(KEY_NAME);//删除获取的码,保留生成的密码 if (retry) {createCipher(false);} else {throw new Exception("Could not create the cipher", e);}}return cipher;}

4.调用 authenticate() 方法启动指纹传感器并开始监听:

CryptoObjectHelper cryptoObjectHelper = new CryptoObjectHelper();if (cancellationSignal == null) {cancellationSignal = new CancellationSignal();}fingerprintManager.authenticate(cryptoObjectHelper.buildCryptoObject(), 0,cancellationSignal, myAuthCallback, null);

最后我们在回调的类中监听指纹识别的结果:

public class MyAuthCallback extends FingerprintManagerCompat.AuthenticationCallback {


private Handler handler = null;


public MyAuthCallback(Handler handler) {super();this.handler = handler;}


/**


  • 验证错误信息*/@Overridepublic void onAuthenticationError(int errMsgId, CharSequence errString) {super.onAuthenticationError(errMsgId, errString);if (handler != null) {handler.obtainMessage(Constant.MSG_AUTH_ERROR, errMsgId, 0).sendToTarget();}}


/**


  • 身份验证帮助*/@Overridepublic void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {super.onAuthenticationHelp(helpMsgId, helpString);if (handler != null) {handler.obtainMessage(Constant.MSG_AUTH_HELP, helpMsgId, 0).sendToTarget();}}


/**


  • 验证成功*/@Overridepublic void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {super.onAuthenticationSucceeded(result);if (handler != null) {handler.obtainMessage(Constant.MSG_AUTH_SUCCESS).sendToTarget();}}


/**


  • 验证失败*/@Overridepublic void onAuthenticationFailed() {super.onAuthenticationFailed();if (handler != null) {handler.obtainMessage(Constant.MSG_AUTH_FAILED).sendToTarget();}}}




好了,上面一直讲的是对称加密以实现指纹识别;


接下来写了一个使用指纹进行登录的 demo 及封装(这里没有使用加密..):

我们先来看下我总结的指纹登录流程

指纹识别一定会有成功、失败等各种情况,所以先定义一个回调监听

/**


  • Description: 指纹识别回调

  • Created by jia on 2017/11/27.

  • 人之所以能,是相信能*/public interface FingerListener {


/**


  • 开始识别*/void onStartListening();


/**


  • 停止识别*/void onStopListening();


/**


  • 识别成功

  • @param result*/void onSuccess(FingerprintManager.AuthenticationResult result);


/**


  • 识别失败*/void onFail(boolean isNormal,String info);


/**


  • 多次识别失败 的 回调方法

  • @param errorCode

  • @param errString*/void onAuthenticationError(int errorCode, CharSequence errString);


/**


  • 识别提示*/void onAuthenticationHelp(int helpCode, CharSequence helpString);


}

1、先封装了指纹工具类

private FingerprintManager manager;private KeyguardManager mKeyManager;private CancellationSignal mCancellationSignal;//回调方法 private FingerprintManager.AuthenticationCallback mSelfCancelled;


private Context mContext;


private FingerListener listener;


指纹识别相关管理类当然是必须的了。

2、初始化它们

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {manager = (FingerprintManager) mContext.getSystemService(Context.FINGERPRINT_SERVICE);mKeyManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);mCancellationSignal = new CancellationSignal();initSelfCancelled();}

3、初始化系统的识别回调

private void initSelfCancelled() {mSelfCancelled = new FingerprintManager.AuthenticationCallback() {@Overridepublic void onAuthenticationError(int errorCode, CharSequence errString) {// 多次指纹密码验证错误后,进入此方法;并且,不能短时间内调用指纹验证 listener.onAuthenticationError(errorCode, errString);}


@Overridepublic void onAuthenticationHelp(int helpCode, CharSequence helpString) {listener.onAuthenticationHelp(helpCode, helpString);}


@Overridepublic void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {listener.onSuccess(result);}


@Overridepublic void onAuthenticati


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


onFailed() {listener.onFail(true, "识别失败");}};}

4、开始识别

/**


  • 开始监听识别*/public void startListening(FingerListener listener) {


this.listener = listener;


if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {listener.onFail(false, "未开启权限");return;}


if (isFinger() == null) {listener.onStartListening();manager.authenticate(null, mCancellationSignal, 0, mSelfCancelled, null);} else {listener.onFail(false, isFinger());}}


注意:ActivityCompat.checkSelfPermission 必须在开始识别前执行,否则编译环境会报错...

5、取消识别

/**


  • 停止识别*/public void cancelListening() {if (mCancellationSignal != null) {mCancellationSignal.cancel();listener.onStopListening();}}

同时也少不了各种情况的判断

/**


  • 硬件是否支持

  • <p/>

  • 返回 null 则可以进行指纹识别

  • 否则返回对应的原因

  • @return*/public String isFinger() {


//android studio 上,没有这个会报错 if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {


//android studio 上,没有这个会报错 if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {return "没有指纹识别权限";}//判断硬件是否支持指纹识别 if (!manager.isHardwareDetected()) {return "没有指纹识别模块";}

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
安卓指纹对称加密及登录功能的实现(1),技术实现