随着科技的进步,不仅是软件方面,安卓系统在硬件发展上也有了日新月异的发展。下半年出的安卓手机基本上都带指纹识别功能,不管是炒得很热的360奇酷手机、魅族手机,还是“中华酷联”旗下的手机都开始加入指纹识别的功能,指纹识别似乎已经成标配,购买手机时考虑是否带有指纹识别功能似乎也成为了消费者的一个购物倾向。
笔者近期就对自己的三星Note 5进行了一些android指纹识别的代码开发。首先,先介绍一下指纹识别的原理:
对称加密、非对称加密和签名
在正式使用指纹识别功能之前,有必要先了解一下对称加密和非对称加密的相关内容。
对称加密:所谓对称,就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密。密钥是控制加密及解密过程的指令。算法是一组规则,规定如何进行加密和解密。因此加密的安全性不仅取决于加密算法本身,密钥管理的安全性更是重要。因为加密和解密都使用同一个密钥,如何把密钥安全地传递到解密者手上就成了必须要解决的问题。
非对称加密:非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)。公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。 非对称加密算法实现机密信息交换的基本过程是:甲方生成一对密钥并将其中的一把作为公用密钥向其它方公开;得到该公用密钥的乙方使用该密钥对机密信息进行加密后再发送给甲方;甲方再用自己保存的另一把专用密钥对加密后的信息进行解密。
签名:在信息的后面再加上一段内容,可以证明信息没有被修改过。一般是对信息做一个hash计算得到一个hash值,注意,这个过程是不可逆的,也就是说无法通过hash值得出原来的信息内容。在把信息发送出去时,把这个hash值加密后做为一个签名和信息一起发出去。
由以上内容可以了解到,对称加密和非对称加密的特点如下:
对称加密的优点是速度快,适合于本地数据和本地数据库的加密,安全性不如非对称加密。常见的对称加密算法有DES、3DES、AES、Blowfish、IDEA、RC5、RC6。
非对称加密的安全性比较高,适合对需要网络传输的数据进行加密,速度不如对称加密。非对称加密应用于SSH, HTTPS, TLS,电子证书,电子签名,电子身份证等等
指纹识别的对称加密实现
使用指纹识别的对称加密功能的主要流程如下:
使用 KeyGenerator 创建一个对称密钥,存放在 KeyStore 里。
设置 KeyGenParameterSpec.Builder.setUserAuthenticationRequired() 为true,
使用创建好的对称密钥初始化一个Cipher对象,并用该对象调用 FingerprintManager.authenticate() 方法启动指纹传感器并开始监听。
重写 FingerprintManager.AuthenticationCallback 的几个回调方法,以处理指纹识别成功(onAuthenticationSucceeded())、失败(onAuthenticationFailed() 和 onAuthenticationError())等情况。
可以说,这项技术是苹果引入的, 苹果收购了AuthenTech,出于一贯的封闭思路,AuthenTech不向其它手机厂商提供类似的指纹识别技术。 所以原理上,这项技术有一些苹果ios开发的渊源。
Android指纹识别上还有一个生态难题:尚未得到Android官方系统的支持,指纹识别技术目前还不能惠及整个生态。除了用于解锁外,手机厂商只能分别去和应用公司合作来推广指纹识别的应用场景,例如三星、华为和Paypal、支付宝的合作,不能取代密码用于Google账号的登陆、Play商店的购买等。不过随着越来越多的Android机型也配置指纹识别,获得官方系统支持只是时间问题,虽然目前的方案还都比不上苹果独占的AuthenTech技术,但指纹识别比官方系统自带的人脸识别,效果可好太多了。
言归正传,代码附上:
1. FingerPrintEnrollBase类:
public interface Listener {void onEnrollmentHelp(CharSequence helpString);void onEnrollmentError(int errMsgId, CharSequence errString);void onEnrollmentProgressChange(int steps, int remaining);}
1) onEnrollmentHelp函数:是下层返回的指纹操作帮助信息。
2)onEnrollmentError函数是下层返回的错误信息,errMsgId是错误信息类型,其中1表示指纹硬件不可用,3表示超时,5表示操作已取消。errString是errMsgId对应的文字信息,有需要的自己实验就知道了。
3)onEnrollmentProgressChange()函数是显示当前指纹的采集情况,steps:总共需要录入多少次;remaining是剩余多少次,我在此函数中有打印Log。
2. FingerPrintActivity是主Activity类,FingerPrintEnrollBase是指纹Base类。
需要特别说明的是,在FingerPrintEnrollBase类中有一句:private byte[] mToken = new byte[69];这个数组正常情况是通过输入密码后经由framework层向hal层申请的,我这里为了省去验证那一块的代码,通过sizeof(hw_auth_token_t)计算出来的为69,需要的请依此计算。我在代码里实验过,此数组如果不是69(在64位CPU上),将直接导致程序crach!
fpc指纹的采集流程图:
FingerPrintEnrollBase:
import android.app.Activity;import android.app.Fragment;import android.content.Context;import android.hardware.fingerprint.FingerprintManager;import android.os.Bundle;import android.os.CancellationSignal;import android.os.Handler;import android.util.Log;/*** local fragment to handle the state around fingerprint enrollment.*/public class FingerPrintEnrollBase extends Fragment { private int mEnrollmentSteps = -1;private int mEnrollmentRemaining = 0;private Listener mListener;private boolean mEnrolling;private CancellationSignal mEnrollmentCancel;private Handler mHandler = new Handler();private byte[] mToken = new byte[69];//if byte length != sizeof(hw_auth_token_t), throws Exceptionprivate boolean mDone;private static final String TAG = "FingerPrintEnrollBase";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setRetainInstance(true);}@Overridepublic void onAttach(Activity activity) {super.onAttach(activity);}@Overridepublic void onStart() {super.onStart();Log.d(TAG, "mToken length="+mToken.length);if (!mEnrolling) {FingerprintManager mFpM = (FingerprintManager) getActivity().getSystemService(Context.FINGERPRINT_SERVICE);startEnrollment(mFpM);}}@Overridepublic void onStop() {super.onStop();if (!getActivity().isChangingConfigurations()) {cancelEnrollment();}}protected void startEnrollment(FingerprintManager mFpM) {mHandler.removeCallbacks(mTimeoutRunnable);mEnrollmentSteps = -1;mEnrollmentCancel = new CancellationSignal();if(mFpM != null){mFpM.enroll(mToken, mEnrollmentCancel, 0, mEnrollmentCallback);}mEnrolling = true;}protected void cancelEnrollment() {mHandler.removeCallbacks(mTimeoutRunnable);if (mEnrolling) {mEnrollmentCancel.cancel();mEnrolling = false;mEnrollmentSteps = -1;}}public void setListener(Listener listener) {mListener = listener;}public int getEnrollmentSteps() {return mEnrollmentSteps;}public int getEnrollmentRemaining() {return mEnrollmentRemaining;}public boolean isDone() {return mDone;}private FingerprintManager.EnrollmentCallback mEnrollmentCallback = new FingerprintManager.EnrollmentCallback() {@Overridepublic void onEnrollmentProgress(int remaining) {if (mEnrollmentSteps == -1) {mEnrollmentSteps = remaining;}mEnrollmentRemaining = remaining;mDone = remaining == 0;if (mListener != null) {mListener.onEnrollmentProgressChange(mEnrollmentSteps, remaining);}}@Overridepublic void onEnrollmentHelp(int helpMsgId, CharSequence helpString) {if (mListener != null) {mListener.onEnrollmentHelp(helpString);}}@Overridepublic void onEnrollmentError(int errMsgId, CharSequence errString) {if (mListener != null) {mListener.onEnrollmentError(errMsgId, errString);}}};private final Runnable mTimeoutRunnable = new Runnable() {@Overridepublic void run() {cancelEnrollment();}};public interface Listener {void onEnrollmentHelp(CharSequence helpString);void onEnrollmentError(int errMsgId, CharSequence errString);void onEnrollmentProgressChange(int steps, int remaining);}}
FingerPrintActivity:
import java.io.FileInputStream; import java.io.IOException; import java.util.List; import java.util.Timer; import java.util.TimerTask; import android.R.color; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.os.CancellationSignal; import android.os.Handler; import android.os.Message; import android.text.Spannable; import android.text.style.ForegroundColorSpan; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TableRow; import android.widget.TextView; import android.widget.Toast; import com.mediatek.test.R; import android.hardware.fingerprint.Fingerprint; import android.hardware.fingerprint.FingerprintManager; public class FingerPrintActivity extends Activity implements FingerPrintEnrollBase.Listener{ private static final String TAG = "FingerPrintActivity"; private MyHandler mHandler = new MyHandler(); private TextView fingerPrintAcquisitionTextView, fingerPrintEnrollmentErrorView, fingerPrintEnrollmentHelpView; private FingerPrintEnrollBase fpeb; private FingerprintManager mFingerprintManager; @Override protected int setTitleId() { return R.string.tittle_fingerprint; } @Override protected Activity getActivity() { return this; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fingerprint); fingerPrintAcquisitionTextView = (TextView)findViewById(R.id.fingerprint_message); fingerPrintEnrollmentErrorView= (TextView)findViewById(R.id.fingerprint_enrollment_error); fingerPrintEnrollmentHelpView= (TextView)findViewById(R.id.fingerprint_enrollment_help); mFingerprintManager = (FingerprintManager) getActivity().getSystemService( Context.FINGERPRINT_SERVICE); } private void sendMsgToHandler(int msgType, String remark){ Message msg = Message.obtain(); msg.what = msgType; Bundle bundle = new Bundle(); bundle.putString("result", remark); msg.setData(bundle); mHandler.sendMessage(msg); } @Override public void onEnrollmentHelp(CharSequence helpString) { Log.d(TAG, "==onEnrollmentHelp="+helpString.toString()); sendMsgToHandler(3, helpString.toString()); } @Override public void onEnrollmentError(int errMsgId, CharSequence errString) { Log.d(TAG, "==onEnrollmentError errMsgId="+errMsgId+" errString="+errString.toString()); if(errMsgId == 3){//time out postEnrollment(); }else if(errMsgId == 1){//hardware lose efficacy startEnrollment(); }else if(errMsgId == 5){//operation cancel }else{ sendMsgToHandler(4, errString.toString()); } } @Override public void onEnrollmentProgressChange(int steps, int remaining) { Log.d(TAG, "===============onEnrollmentProgressChange======="); Log.d(TAG, "steps" + steps + "||remaining=" + remaining); fingerPrintEnrollmentHelpView.setText(""); fingerPrintEnrollmentErrorView.setText(""); sendMsgToHandler(2, getString(R.string.fingerprint_validate_success)); buttonS.setEnabled(true); stopFingerprint(); } private void startEnrollment(){ if (fpeb == null) { fpeb = new FingerPrintEnrollBase(); } fpeb.setListener(this); fpeb.startEnrollment(mFingerprintManager); } private void postEnrollment(){ if(mFingerprintManager != null){ mFingerprintManager.postEnroll(); } } private void stopFingerprint() { if (fpeb != null){ fpeb.cancelEnrollment(); onStop(); } } @Override protected void onStart() { super.onStart(); } @Override protected void onStop() { super.onStop(); if(fpeb != null){ fpeb.setListener(null); } } @Override protected void onResume() { super.onResume(); if(mFingerprintManager == null){ mFingerprintManager = (FingerprintManager) getActivity().getSystemService( Context.FINGERPRINT_SERVICE); } startEnrollment(); } @Override protected void onPause() { super.onPause(); postEnrollment(); mFingerprintManager = null; stopFingerprint(); fpeb = null; } class MyHandler extends Handler{ @Override public void handleMessage(Message msg) { Bundle bundle = null; super.handleMessage(msg); switch (msg.what) { case 0: removeMessages(0); bundle = msg.getData(); String result = bundle.getString("result"); selfTestView.setText(result); selfTestView.setTextColor(result.contains("PASS")?Color.GREEN:Color.RED); break; case 1: removeMessages(1); bundle = msg.getData(); String rs = bundle.getString("result"); checkBroadView.setText(bundle.getString("result")); checkBroadView.setTextColor(rs.contains("PASS")?Color.GREEN:Color.RED); break; case 2: removeMessages(2); bundle = msg.getData(); String fingerprint = bundle.getString("result"); fingerPrintAcquisitionTextView.setText(bundle.getString("result")); fingerPrintAcquisitionTextView.setTextColor(fingerprint.contains("PASS")?Color.GREEN:Color.WHITE); mButtonSelftest.setEnabled(true); mButtonCheckerboardTest.setEnabled(true); break; case 3: removeMessages(3); bundle = msg.getData(); String enrollmentHelp = bundle.getString("result"); fingerPrintEnrollmentHelpView.setText(bundle.getString("result")); break; case 4: removeMessages(4); bundle = msg.getData(); String enrollmentError = bundle.getString("result"); fingerPrintEnrollmentErrorView.setText(bundle.getString("result")); fingerPrintAcquisitionTextView.setText(""); break; default:break;}}}}
原文:/android指纹识别源码/
如果觉得《android指纹识别源码》对你有帮助,请点赞、收藏,并留下你的观点哦!