I don't think you should really care about the size of the password because you don't know how the encryption algorithm will generate the password for you. API Keys for instance or crypted password can be very length (I have seen for instance some encrypted API key that have almost 900 chars).
The computers, the languages, the databases and the network in this modern world are adapted for this kind of situation.
Also, for security issue, your password have to be very strong.
- For Java, use
String
it can support 2,147,483,647 characters. IMO you can use 120 or 128 chars for the password size.
- For your database, MySQL for example (or any database) use Text types and its associated sub types.
Spring Security may create proper data properties, but even so, I want
to know the algorithm and the length for password field.
You can define a custom encryption in Spring Security with a bean, in you configuration file. The following is the list of all the algorithms supported by Spring Security: NoOpPasswordEncoder, StandardPasswordEncoder, Pbkdf2PasswordEncoder, BCryptPasswordEncoder, SCryptPasswordEncoder
@Bean
public PasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
Take in consideration that you have 2 sides when managing your security and applying encryption: the user side password and the encrypted password stored in the database. When the user issue an authentication process, the user type his password, not the encrypted password. The verification is made in a AuthenticationProvider
that you define.
See an example bellow:
@Service
public class UserAuthenticationService implements AuthenticationProvider {
private UserDetailsService service;
private PasswordEncoder encoder;
public UserAuthenticationService(UserDetailsService service, PasswordEncoder encoder) {
this.service = service;
this.encoder = encoder;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
var username = authentication.getName();
var password = authentication.getCredentials().toString();
var user = service.loadUserByUsername(username);
// You make this comparison here, between both password the user and the encrypted one in the database
if (!encoder.matches(password, user.getPassword())) {
throw new BadCredentialsException("Bad credentials");
}
return new UsernamePasswordAuthenticationToken(
user.getUsername(),
user.getPassword(),
user.getAuthorities()
);
}
@Override
public boolean supports(Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
}
}