Android指紋識(shí)別

上一篇講了通過FingerprintManager驗(yàn)證手機(jī)是否支持指紋識(shí)別,以及是否錄入了指紋,這里進(jìn)行指紋的驗(yàn)證.

//獲取FingerprintManager實(shí)例
FingerprintManager mFingerprintManager =
                                                (FingerprintManager) context.getSystemService(Context.FINGERPRINT_SERVICE);
//執(zhí)行驗(yàn)證監(jiān)聽
mFingerprintManager
                .authenticate(cryptoObject, mCancellationSignal, 0, this, null);

參數(shù)說明:

cryptoObject//FingerprintManager支持的加密對象的包裝類。目前該框架支持Signature,Cipher和Mac對象。
mCancellationSignal//提供取消正在進(jìn)行的操作的功能。
callback(參數(shù)中的this)//指紋識(shí)別的回調(diào)函數(shù)

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);

回調(diào)函數(shù):

@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
    //驗(yàn)證出現(xiàn)錯(cuò)誤了
    //errString為錯(cuò)誤的信息
}

@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
    showError(helpString);
    //驗(yàn)證出現(xiàn)一些問題的系統(tǒng)提示,比如:請按久一點(diǎn)等提示信息.
}

@Override
public void onAuthenticationFailed() {
    showError("指紋驗(yàn)證失敗");
    //在驗(yàn)證失敗和出現(xiàn)問題以后,系統(tǒng)會(huì)繼續(xù)執(zhí)行監(jiān)聽,使用者需要在這里修改相關(guān)提示信息
}

@Override
public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
    //驗(yàn)證成功
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 相信大部分人都用過指紋識(shí)別,日常生活中很多場景用到指紋識(shí)別技術(shù),比如手機(jī)解鎖,指紋支付,指紋打卡,就連公司附近...
    Peak_jianshu閱讀 5,389評論 0 2
  • ??常用開發(fā)工具類和自定義view,無恥的求個(gè)star:??https://github.com/AbrahamC...
    想你依然心痛閱讀 5,002評論 1 4
  • 點(diǎn)擊查看原文 Web SDK 開發(fā)手冊 SDK 概述 網(wǎng)易云信 SDK 為 Web 應(yīng)用提供一個(gè)完善的 IM 系統(tǒng)...
    layjoy閱讀 14,290評論 0 15
  • 最近項(xiàng)目需要使用到指紋識(shí)別的功能,查閱了相關(guān)資料后,整理成此文。 指紋識(shí)別是在Android 6.0之后新增的功能...
    湫水長天閱讀 3,879評論 2 46
  • 初遇 初遇貍貓的時(shí)候我還是個(gè)不大懂事的孩子,小學(xué)二年級的小學(xué)生而已。之所以會(huì)記得比較清楚大概是因?yàn)槟鞘俏移诖撕芫?..
    LL隨安閱讀 488評論 0 1

友情鏈接更多精彩內(nèi)容