Android下的指紋識別是在Android6.0後添加的功能,所以,在實現的時候要判斷用戶機是否支持,而後對於開發來講,使用場景有兩種,分別是本地識別和跟服務器交互;android
不管是本地仍是與服務器交互,都須要對信息進行加密,一般來講,與本地交互的採用對稱加密,與服務器交互則採用非對稱加密,下面咱們來簡單介紹下對稱加密和非對稱加密算法
採用單密鑰密碼系統的方法,同一密鑰做爲加密和解密的工具,經過密鑰控制加密和解密餓的指令,算法規定如何加密和解密。優勢是算法公開、加密解密速度快、效率高,缺點是發送前的雙方保持統一密鑰,若是泄露則不安全,一般由AES、DES加密算法等;安全
非對稱加密算法須要兩個密鑰來進行加密和解密,這兩個祕鑰是公開密鑰(簡稱公鑰)和私有密鑰(簡稱私鑰),若是一方用公鑰進行加密,接受方應用私鑰進行解密,反之發送方用私鑰進行加密,接收方用公鑰進行解密,因爲加密和解密使用的不是同一密鑰,故稱爲非對稱加密算法;與對稱加密算法相比,非對稱加密的安全性獲得了很大的提高,可是效率上則低了不少,由於解密加密花費的時間更長了,因此適合數據量少的加密,一般有RSA,ECC加密算法等等bash
首先咱們判斷手機是否支持指紋識別,是否有相關的傳感器,是否錄入了相關指紋,而後纔開始對指紋作出系列的操做;服務器
fingerprintManager = FingerprintManagerCompat.from(this);
if (!fingerprintManager.isHardwareDetected()) {
//是否支持指紋識別
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("沒有傳感器");
builder.setCancelable(false);
builder.create().show();
} else if (!fingerprintManager.hasEnrolledFingerprints()) {
//是否已註冊指紋
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("沒有註冊指紋");
builder.setCancelable(false);
builder.create().show();
} else {
try {
//這裏去新建一個結果的回調,裏面回調顯示指紋驗證的信息
myAuthCallback = new MyAuthCallback(handler);
} catch (Exception e) {
e.printStackTrace();
}
}
複製代碼
這裏初始化handle對應指紋識別完成後發送過來的消息微信
private void initHandler() {
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
//識別成功
case MSG_AUTH_SUCCESS:
setResultInfo(R.string.fingerprint_success);
mCancelBtn.setEnabled(false);
mStartBtn.setEnabled(true);
cancellationSignal = null;
break;
//識別失敗
case MSG_AUTH_FAILED:
setResultInfo(R.string.fingerprint_not_recognized);
mCancelBtn.setEnabled(false);
mStartBtn.setEnabled(true);
cancellationSignal = null;
break;
//識別錯誤
case MSG_AUTH_ERROR:
handleErrorCode(msg.arg1);
break;
//幫助
case MSG_AUTH_HELP:
handleHelpCode(msg.arg1);
break;
}
}
};
}
複製代碼
對稱加密的主要實現步驟以下:ide
/**
* 建立keystore
* @throws Exception
*/
public CryptoObjectHelper() throws Exception {
KeyStore _keystore = KeyStore.getInstance(KEYSTORE_NAME);
_keystore.load(null);
}
複製代碼
/**
* 獲取祕鑰生成器,用於生成祕鑰
* @throws Exception
*/
public void CreateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHM, KEYSTORE_NAME);
KeyGenParameterSpec keyGenSpec =
new KeyGenParameterSpec.Builder(KEY_NAME, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(BLOCK_MODE)
.setEncryptionPaddings(ENCRYPTION_PADDING)
.setUserAuthenticationRequired(true)
.build();
keyGen.init(keyGenSpec);
keyGen.generateKey();
}
複製代碼
/**
* @throws Exception
* 密碼生成,遞歸實現
*/
Cipher createCipher(boolean retry) throws Exception {
Key key = GetKey();
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
try {
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;
}
複製代碼
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;
}
/**
* 驗證錯誤信息
*/
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
super.onAuthenticationError(errMsgId, errString);
if (handler != null) {
handler.obtainMessage(Constant.MSG_AUTH_ERROR, errMsgId, 0).sendToTarget();
}
}
/**
* 身份驗證幫助
*/
@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
super.onAuthenticationHelp(helpMsgId, helpString);
if (handler != null) {
handler.obtainMessage(Constant.MSG_AUTH_HELP, helpMsgId, 0).sendToTarget();
}
}
/**
* 驗證成功
*/
@Override
public void onAuthenticationSucceeded(FingerprintManagerCompat.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
if (handler != null) {
handler.obtainMessage(Constant.MSG_AUTH_SUCCESS).sendToTarget();
}
}
/**
* 驗證失敗
*/
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
if (handler != null) {
handler.obtainMessage(Constant.MSG_AUTH_FAILED).sendToTarget();
}
}
}
複製代碼
好了,上面一直講的是對稱加密以實現指紋識別;工具
接下來寫了一個使用指紋進行登陸的demo及封裝(這裏沒有使用加密..):ui
/**
* 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);
}
複製代碼
private FingerprintManager manager;
private KeyguardManager mKeyManager;
private CancellationSignal mCancellationSignal;
//回調方法
private FingerprintManager.AuthenticationCallback mSelfCancelled;
private Context mContext;
private FingerListener listener;
複製代碼
指紋識別相關管理類固然是必須的了。this
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();
}
複製代碼
private void initSelfCancelled() {
mSelfCancelled = new FingerprintManager.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode, CharSequence errString) {
// 屢次指紋密碼驗證錯誤後,進入此方法;而且,不能短期內調用指紋驗證
listener.onAuthenticationError(errorCode, errString);
}
@Override
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
listener.onAuthenticationHelp(helpCode, helpString);
}
@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
listener.onSuccess(result);
}
@Override
public void onAuthenticationFailed() {
listener.onFail(true, "識別失敗");
}
};
}
複製代碼
/**
* 開始監聽識別
*/
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必須在開始識別前執行,不然編譯環境會報錯...
/**
* 中止識別
*/
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 "沒有指紋識別模塊";
}
//判斷 是否開啓鎖屏密碼
if (!mKeyManager.isKeyguardSecure()) {
return "沒有開啓鎖屏密碼";
}
//判斷是否有指紋錄入
if (!manager.hasEnrolledFingerprints()) {
return "沒有錄入指紋";
}
}
return null;
}
複製代碼
/**
* 檢查SDK版本
*
* @return
*/
public boolean checkSDKVersion() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return true;
}
return false;
}
複製代碼
開啓指紋登陸
登陸識別
好了,指紋識別大概是這樣了。