在 Android 上使用 JBCrypt 进行散列时出现错误

I'm getting an error when hashing with JBCrypt on Android

我正在用 BCrypt 进行一些哈希测试,我有下一个代码:

EditText toEnc;
Button btnEnc;
TextView encrypted;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toEnc = findViewById(R.id.toEncrypt);
    btnEnc = findViewById(R.id.btnEncrypt);
    encrypted = findViewById(R.id.encrypted);

    btnEnc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String encrypt = BCrypt.hashpw(toEnc.getText().toString().trim(), BCrypt.gensalt(10));
            encrypted.setText(encrypt);
        }
    });

}

首先,我也有一个加密函数来生成要散列的字符串,但是当我遇到这个错误时,我决定只尝试使用用户提供的字符串。

实际上我已经从 maven 的存储库下载了这个库,在这里: https://mvnrepository.com/artifact/org.mindrot/jbcrypt/0.4

我将其添加为 .jar

现在,我尝试在 build.gradle 中添加这种依赖:

编译'org.mindrot:jbcrypt:0.4'

当我点击按钮对引入的字符串进行哈希处理时发生错误。

应用程序仅停止。

我收到下一条消息:

W/Binder: Caught a RuntimeException from the binder stub implementation.
java.lang.NullPointerException: Attempt to invoke interface method  android.os.IBinder com.mediatek.anrappmanager.IFrameworks.serviceManagerGetService(java.lang.String)' on a null object reference
              at com.mediatek.anrappmanager.ANRManagerNative.b(SourceFile:77)
              at com.mediatek.anrappmanager.ANRManagerNative.c(SourceFile:75)
              at com.mediatek.anrappmanager.ANRManagerNative$a.get(SourceFile:97)
              at com.mediatek.anrappmanager.ANRManagerNative.getDefault(SourceFile:35)
              at com.mediatek.anrappmanager.ANRAppManager.dumpMessageHistory(SourceFile:59)
              at android.app.ActivityThread$ApplicationThread.dumpMessageHistory(ActivityThread.java:1244)
              at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:682)
              at android.os.Binder.execTransact(Binder.java:451)
W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'android.os.IBinder com.mediatek.anrappmanager.IFrameworks.serviceManagerGetService(java.lang.String)' on a null object reference
W/System.err:     at com.mediatek.anrappmanager.ANRManagerNative.b(SourceFile:77)
W/System.err:     at com.mediatek.anrappmanager.ANRManagerNative.c(SourceFile:75)
W/System.err:     at com.mediatek.anrappmanager.ANRManagerNative$a.get(SourceFile:97)
W/System.err:     at com.mediatek.anrappmanager.ANRManagerNative.getDefault(SourceFile:35)
W/System.err:     at com.mediatek.anrappmanager.ANRAppManager.dumpMessageHistory(SourceFile:59)
W/System.err:     at android.app.ActivityThread$ApplicationThread.dumpMessageHistory(ActivityThread.java:1244)
W/System.err:     at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:682)
W/System.err:     at android.os.Binder.execTransact(Binder.java:451)
I/art: Thread[5,tid=4775,WaitingInMainSignalCatcherLoop,Thread*=0xb8d5a1e0,peer=0x12c000a0,"Signal Catcher"]: reacting to signal 3
I/art: Wrote stack traces to '/data/anr/traces.txt'
Application terminated.

我将代码放在 try-catch 中并且它有效,问题是散列过程比正常情况花费更多时间,所以我要做的是放置一个 ProgressDialog 以避免用户在散列过程中与应用程序交互。

希望对其他人有用。