I use the Spring security for Java web applications and I have written an authentication provider which is working without salt and now I want to either add salt or alternatively use the built-in algorithms with Spring security that appear to be SHA and that can use salt that is specified with XML. In my case I didn't use SHA but AES and the input password is matched against cryptographic string that were encoded e.g. �[�s�E�
qh�%"5%��g"2T��$�` that will match against back-end in this handler.
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
AdministratorAccount administratorAccount = administratorAccountService.get(name);
CryptographyProviderAES256 aes256CryptographyProvider = new CryptographyProviderAES256(256);
byte[] mockSecretCode = new StringBuilder().append("qwertyuiqwertyui").toString().getBytes();
byte[] encrypted = aes256CryptographyProvider.encrypt(password.getBytes(), mockSecretCode);
//TODO: salt
if (administratorAccount.getPassword().equals(new String(encrypted))) {
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
return auth;
} else {
return null;
}
}
Should I use SHA instead that is built-in with Spring security and can be added with XML instead? My Account class has variables for password and salt, how should I implement a salt? Should I generate a salt from time + random when creating the account like this:
final Random r = new SecureRandom();
byte[] salt = new byte[32];
r.nextBytes(salt);