2017-07-19 44 views

回答

0

請使用以下內容。 公共類FingerprintUtils {

private static final String KEY_NAME = "PS"; 
KeyguardManager keyguardManager; 
FingerprintManager fingerprintManager; 
Context context; 
private KeyStore keyStore; 
private Cipher cipher; 


public FingerprintUtils(Context context) { 
    this.context = context; 

} 


public void checkFingerprintValidations(FPValidationCallback callback) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     keyguardManager = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE); 
     fingerprintManager = (FingerprintManager) context.getSystemService(FINGERPRINT_SERVICE); 
     if (!fingerprintManager.isHardwareDetected()) { 
      //Log.e("Test", "Your Device does not have a Fingerprint Sensor"); 
      callback.isHardwareAvailFalse(); 
      return; 
     } 
     if (ActivityCompat.checkSelfPermission(context, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) { 
      //Log.e("Test", "Fingerprint authentication permission not enabled"); 
      callback.isPermissionPermittedFalse(); 
      return; 
     } 
     if (!fingerprintManager.hasEnrolledFingerprints()) { 
      //Log.e("Test", "Register at least one fingerprint in Settings"); 
      callback.isFingerRegisteredFalse(); 
      return; 
     } 
     if (!keyguardManager.isKeyguardSecure()) { 
      //Log.e("Test", "Lock screen security not enabled in Settings"); 
      callback.isLockScreenSecurityFalse(); 
      return; 
     } 

     callback.continueOnFingerprint(); 
    } else { 
     callback.isDevice23False(); 
    } 


} 

public void matchFingerPrintsNow(FPResultCallback FPResultCallback) { 

    generateKey(); 

    if (cipherInit()) { 
     FingerprintManager.CryptoObject cryptoObject = new FingerprintManager.CryptoObject(cipher); 
     FingerprintHandler helper = new FingerprintHandler(context); 

     helper.startAuth(fingerprintManager, cryptoObject, FPResultCallback); 
    } 

} 


//--------------------------------------Used in this class private access------------------ 
private void generateKey() { 
    try { 
     keyStore = KeyStore.getInstance("AndroidKeyStore"); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    KeyGenerator keyGenerator; 
    try { 
     keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"); 
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) { 
     throw new RuntimeException("Failed to get KeyGenerator instance", e); 
    } 

    try { 
     keyStore.load(null); 
     keyGenerator.init(new 
       KeyGenParameterSpec.Builder(KEY_NAME, 
       KeyProperties.PURPOSE_ENCRYPT | 
         KeyProperties.PURPOSE_DECRYPT) 
       .setBlockModes(KeyProperties.BLOCK_MODE_CBC) 
       .setUserAuthenticationRequired(true) 
       .setEncryptionPaddings(
         KeyProperties.ENCRYPTION_PADDING_PKCS7) 
       .build()); 
     keyGenerator.generateKey(); 
    } catch (NoSuchAlgorithmException | 
      InvalidAlgorithmParameterException 
      | CertificateException | IOException e) { 
     throw new RuntimeException(e); 
    } 
} 


private boolean cipherInit() { 
    try { 
     cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7); 
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { 
     throw new RuntimeException("Failed to get Cipher", e); 
    } 

    try { 
     keyStore.load(null); 
     SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME, 
       null); 
     cipher.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); 
    } 
} 

}

fingerprintUtils.checkFingerprintValidations(new FPValidationCallback() { 
     @Override 
     public void isHardwareAvailFalse() { 
      updateText("isHardwareAvailFalse"); 
     } 

     @Override 
     public void isPermissionPermittedFalse() { 
      updateText("isPermissionPermittedFalse"); 
     } 

     @Override 
     public void isFingerRegisteredFalse() { 
      updateText("isFingerRegisteredFalse"); 
     } 

     @Override 
     public void isLockScreenSecurityFalse() { 
      updateText("isLockScreenSecurityFalse"); 
     } 

     @Override 
     public void isDevice23False() { 
      updateText("isDevice23False"); 
     } 

     @Override 
     public void continueOnFingerprint() { 
      fingerprintUtils.matchFingerPrintsNow(new FPResultCallback() { 
       @Override 
       public void onFingerPrintError(int errMsgId, CharSequence errString) { 
        updateText("Error " /*+ errMsgId + " "*/ + errString.toString()); 
       } 

       @Override 
       public void onFingerPrintHelp(int helpMsgId, CharSequence helpString) { 
        updateText("onFingerPrintHelp "/* + helpMsgId + " "*/ + helpString.toString()); 
       } 

       @Override 
       public void onFingerPrintFailed() { 
        updateText("FingerPrintFailed"); 
       } 

       @Override 
       public void onFingerPrintSucceeded(FingerprintManager.AuthenticationResult result) { 
        statusTV.setText("Authentication Successful!"); 
        Intent in = new Intent(); 
        setResult(RESULT_OK, in); 
        finish(); 
       } 
      }); 
     } 
    }); 

公共接口FPValidationCallback {

void isHardwareAvailFalse(); 
void isPermissionPermittedFalse(); 
void isFingerRegisteredFalse(); 
void isLockScreenSecurityFalse(); 
void isDevice23False();` 
void continueOnFingerprint(); 

}