Java 如何等待生物特征提示。身份验证(PrompInfo,cryptoObject)在函数中完成身份验证任务?

Java 如何等待生物特征提示。身份验证(PrompInfo,cryptoObject)在函数中完成身份验证任务?,java,android,android-studio,android-biometric-prompt,Java,Android,Android Studio,Android Biometric Prompt,我正在编写一个Android sign函数,它接受字节[]明文并返回字节[]签名。它使用安卓手机中安全元素的私有部分进行签名,这需要生物认证(biometricPrompt.authentication) 我尝试了很多方法,但每次都失败了。下面的代码在运行期间不显示生物识别提示,它只是因为生物识别提示而冻结在主屏幕上。wait()。我已经寻找了很多解决方案,但没有找到。请帮忙。:) 非常确定您必须在后台线程上调用.wait(),以确保它不会阻止UI线程。非常确定您必须在后台线程上调用.wait(

我正在编写一个Android sign函数,它接受字节[]明文并返回字节[]签名。它使用安卓手机中安全元素的私有部分进行签名,这需要生物认证(biometricPrompt.authentication)

我尝试了很多方法,但每次都失败了。下面的代码在运行期间不显示生物识别提示,它只是因为生物识别提示而冻结在主屏幕上。wait()。我已经寻找了很多解决方案,但没有找到。请帮忙。:)


非常确定您必须在后台线程上调用
.wait()
,以确保它不会阻止UI线程。非常确定您必须在后台线程上调用
.wait()
,以确保它不会阻止UI线程。
  private MainActivityTest mainAct = null;
  private byte[] sig = null;

  public AndroidSecureElement(MainActivity mainAct){
    this.mainAct = mainAct;
  }

  public byte[] signSE(final String alias, final byte[] plaintext) throws Exception {
    final Executor executor;
    final BiometricPrompt biometricPrompt;
    final BiometricPrompt.PromptInfo promptInfo;

    KeyStore keyStore = null;
    final Signature s;
    PrivateKey privKey = null;

    keyStore = KeyStore.getInstance("AndroidKeyStore");
    keyStore.load(null);

    privKey = (PrivateKey)keyStore.getKey(alias, null);

    if(privKey.getAlgorithm().equalsIgnoreCase("RSA")){
        s = Signature.getInstance("SHA256withRSA");
    }
    else if(privKey.getAlgorithm().equalsIgnoreCase("EC")){
        s = Signature.getInstance("SHA256withECDSA");
    }
    else{
        throw new Exception("Sign Failed: Unknown Algorithm found: "+privKey.getAlgorithm());
    }

    privKey = (PrivateKey)keyStore.getKey(alias, null);
    s.initSign(privKey);

    executor = ContextCompat.getMainExecutor(mainAct);
    biometricPrompt = new BiometricPrompt(mainAct,
            executor, new BiometricPrompt.AuthenticationCallback() {
        @Override
        public void onAuthenticationError(int errorCode,
                                          @NonNull CharSequence errString) {
            super.onAuthenticationError(errorCode, errString);
            Toast.makeText(mainAct.getApplicationContext(),
                    "Authentication error: " + errString, Toast.LENGTH_SHORT)
                    .show();
            this.notify();
        }

        @Override
        public void onAuthenticationSucceeded(
                @NonNull BiometricPrompt.AuthenticationResult result) {

            super.onAuthenticationSucceeded(result);
            Toast.makeText(mainAct.getApplicationContext(),
                    "Authentication succeeded!", Toast.LENGTH_SHORT).show();

            try {
                Signature s = result.getCryptoObject().getSignature();
                s.update(plaintext);
                sig = s.sign();
            } catch (Exception e) {
                Toast.makeText(mainAct.getApplicationContext(),
                        "Sign failed!", Toast.LENGTH_SHORT).show();
            }
            finally{
                this.notify();
            }
        }

        @Override
        public void onAuthenticationFailed() {
            super.onAuthenticationFailed();
            Toast.makeText(mainAct.getApplicationContext(), "Authentication failed",
                    Toast.LENGTH_SHORT)
                    .show();
            this.notify();
        }
    });

    promptInfo = new BiometricPrompt.PromptInfo.Builder()
            .setTitle("Biometric login for Secure Element access")
            .setSubtitle("Log in using your biometric credential")
            .setNegativeButtonText("Cancel")
            .setConfirmationRequired(true)
            .build()
    ;

    biometricPrompt.authenticate(promptInfo, new BiometricPrompt.CryptoObject(s));

    synchronized (biometricPrompt) {
        biometricPrompt.wait();
    }

    return sig;
  }