My current method of registering and authenticating a User is as follows:
class User{
private String username;
private String password;
private String salt;
}
Registeration :
addUser(String username, String password){
user.setUsername(username);
user.setSalt(BCrypt.gensalt());
user.setPassword(BCrypt.hashpw(password, user.getSalt()));
}
Authentication :
authenticate(User user){
User userDB = getUserFromDB(user.getUsername());
userDB.getPassword().equals(BCrypt.hashpw(user.getPassword(), userDB.getSalt()))
}
However I am looking at Spring Boot authentication systems and I see people recommending using BCryptPasswordEncoder which only uses password and no salt.
I needed to know if my existing method is better or worse than the one being recommended.
BCryptPasswordEncoder
uses a salt. From the documentation:From the source:
BCryptPasswordEncoder
generates its own salt upon encoding. This is what the optionalSecureRandom
constructor argument is used for. The output of BCrypt includes the salt, which is how a raw password is verified. This is why thematches
method requires the already encoded password.Sample output from bcrypt "example":
The output of bcrypt includes:
2y
indicates Blowfish cipher10
rounds (actually 2^10 rounds)$
are the salt, remainder are the hashPHP documentation describes bcrypt output:
Your method does the same thing, but you are storing the salt explicitly by itself. Instead of storing the salt separately, you can simply use the salt that is stored in the bcrypt output, as described in the method
public static boolean checkpw(String plaintext, String hashed)