I'm working on encrypting passwords for my application since the password will be stored in the shared preferences
I found bcrypt and read a lot of good things about it but I can not get it to work开发者_如何学C
I'm using jBCrypt. I followed the instructions and did this as a test
String hashed = BCrypt.hashpw("dog", BCrypt.gensalt(12));
String candidate = BCrypt.hashpw("dog", BCrypt.gensalt(12));
if (BCrypt.checkpw(candidate, hashed)){
Toast.makeText(Loader.this, "equals", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(Loader.this, "don't match?", Toast.LENGTH_LONG).show();
}
However everytime i run the application the toast that displays is don't match? So when I log the hashed password in my shared prefs and then compare it against the user input it says it would say wrong everytime since apparently it's giving me a different hash everytime what's up how can i use this?
According to the documentation, BCrypt.checkpw()
takes the plaintext password as its first argument. So it should be:
String hashed = BCrypt.hashpw("dog", BCrypt.gensalt(12));
String candidate = "dog";
if (BCrypt.checkpw(candidate, hashed)) {
Toast.makeText(Loader.this, "equals", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(Loader.this, "doesn't match?", Toast.LENGTH_LONG).show();
}
精彩评论