0

So I have stored password as hash in shared preferences, when user puts password I need to make a hash of it and compare with stored one.

Should it be done in AsyncTask or Thread because calculation and comparison could freeze UI? And then do you know a clean way to recieve result (true, false) from asynctask or thread?

public void startGenerateCode(View view) {
    String pinCompare = pin; //pin is class variable obtained from editText
    pinCompare = tools.bin2hex(tools.getHash(pinCompare)); 

    if(pinCompare.compareTo(session.getDetails("Pin"))==0){
        generateCode();
    }
    else
        Toast.makeText(this, "Wrong PIN", Toast.LENGTH_SHORT).show();

}

public void generateCode(){
    Intent i = new Intent(this, GeneratedCode.class);
    startActivity(i);
    overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
    finish();
}

This is done in activity after button is pressed.

Zolo
  • 176
  • 1
  • 2
  • 14

1 Answers1

2

Zolo,

I guess this process is triggered when someone presses a Button, such as login. I don't think you need any extra Thread to process the Hash calculus.

If you then have to connect to a server and send/receive data, then you should use it due to the asynchronous flow.

Response to comments on main post: Yes, you can start an Activity in onPostExecute.

Code example:

public void startGenerateCode(View view) {

    // Disable button         
    Button button = (Butto) view;
    button.setEnabled(false);

    String pinCompare = pin; //pin is class variable obtained from editText
    pinCompare = tools.bin2hex(tools.getHash(pinCompare)); 

    if(pinCompare.compareTo(session.getDetails("Pin"))==0){
        generateCode();
    } else {
        // If the login fails, re-enable the button to try again
        button.setEnabled(true);
        Toast.makeText(this, "Wrong PIN", Toast.LENGTH_SHORT).show();
    }
}

I did it by heart, so there may be mistakes.

JonZarate
  • 841
  • 6
  • 28
  • Yes, process is triggered after Button press. But I did it like this and once I tried app, and pressed button it started the comparison and new activity has not started I pressed it again and when I was pressing back button I had that activity twice in ActivityStack... I was not able to do it again, but it has happend. – Zolo Feb 18 '17 at 12:15
  • @Zolo That's another problem. If you want help on that, at least post your code. – JonZarate Feb 18 '17 at 12:17
  • 1
    @Zolo I think it's short. If the time it takes is less that 1 second, there is nothing to worry about. You can add some defensive programming, and disable the button after someone presses it so that there are no multiple activities launched. I edited my answer with the example. – JonZarate Feb 18 '17 at 12:37
  • @Zolo just added the example, check it out. – JonZarate Feb 18 '17 at 12:40