#Java Spring boot - password hashing
1 messages · Page 1 of 1 (latest)
ur already "doing it wrong" by having the pw as a string. strings are easy too steal
when its about security: dont roll ur own solution
spring has all that stuff built-in, make use of it
@nocturne elk yea I'm trying to encrypt the password using some security library
Bcrypt is the industry standard
if you want a bit more costly / customizable , look into Argon ig
nvm u said encryption not hashing
ehh I mean you should prefer hashing but if you insist on encryption , you could try bouncy castle , it has AES and RSA
I'm looking at the BCryptlibrary for Spring Boot
its for hashing
ok thanks!
yes but the password is a string
instead of a char[]
so u already lost
theres more to consider and ull step into the next pitfall right away
dont do security urself
spring can do auth and login and whatnot fully automatic. ur reinventing security wheels in an unsecure way
bcrypt is ok. I prefer sha256
where is the code gone
i use sha256 for integrity stuff mainly, for auth bcrypt or argon are better since more costly, so cracking it is way harder , especially when salted
Don't worry guys I'm already using encrpytion here:
public UserDataTransferObject createUser(UserDataTransferObject userDto, String password) throws NoSuchAlgorithmException {
if (password == null || password.isBlank()) throw new IllegalArgumentException("Password required!");
String passwordHash = BCrypt.hashpw(password, BCrypt.gensalt());
var user = convertToEntity(userDto);
var existsEmail = repository.existsByEmail(userDto.getEmail());
if (existsEmail) throw new BadRequestException("Email " + user.getEmail() + " is taken!");
var existsUsername = repository.existsByUserName(userDto.getUserName());
if (existsUsername) throw new BadRequestException("Username " + user.getUserName() + " is taken!");
var existsPhone = repository.existsByPhoneNumber(userDto.getPhoneNumber());
if (existsPhone) throw new BadRequestException("Phone number " + user.getPhoneNumber() + "already exists!");
user.setPassword(passwordHash);
System.out.println("Password match"+ BCrypt.checkpw(password, passwordHash));
repository.save(user);
return convertToData(user);
}
Why arent you using springs functionality like PasswordEncoder? Are you even using UserDetailsService?
Why do you have a seperate field for password, why not have it inside the Dto
hes prolly new
and not using spring security
nvm he is using it
@fervent ore Spring Security provides a wide range of pre built stuff that you should use, its super secure and done need manual initializations, just proper config...
@mortal topaz yes I am using it here