io.lsn.spring.auth.PasswordEncoder Maven / Gradle / Ivy
package io.lsn.spring.auth;
import io.lsn.spring.auth.entity.User;
import java.security.NoSuchAlgorithmException;
/**
* @author Patryk Szlagowski
*/
public class PasswordEncoder {
/**
* return true, if given plain password is equal to encoded user password from entity
*
* @param password
* @param user
* @return
*/
public static boolean match(String password, User user) {
if (password == null) {
return false;
}
String encoded = null;
try {
encoded = PasswordEncoder.encode(password, user);
} catch (NoSuchAlgorithmException e) {
return false;
}
// ignore case because of hash codes
return encoded.equalsIgnoreCase(user.getPassword());
}
/**
* encode user password with passwd+salt by using sha256
*
* @param password
* @param user
* @return
* @throws NoSuchAlgorithmException
*/
public static String encode(String password, User user) throws NoSuchAlgorithmException {
return HashGenerator.sha256(password.concat(user.getPasswordSalt()));
}
}