import org.apache.commons.codec.digest.Crypt;
import org.mxchange.jcore.BaseFrameworkSystem;
+import org.mxchange.jusercore.container.login.LoginContainer;
/**
* An utilities class for customers
* @return Hashed user password
*/
public static String encryptPassword (final String userPassword) {
+ // Is it null or empty?
+ if (null == userPassword) {
+ // Throw NPE
+ throw new NullPointerException("userPassword is null");
+ } else if (userPassword.isEmpty()) {
+ // Empty passwords are hardcoded not allowed due to security risks
+ throw new IllegalArgumentException("userPassword is empty");
+ }
+
// Generate large number
String number = Long.toString(Math.round(Math.random() * 10_000_000_000L));
return encryptedPassword;
}
+ /**
+ * Checks if password from container matches the updatedUser's password
+ * <p>
+ * @param container Container holding user instance and unencrypted password
+ * @param updatedUser Updated user instance from database
+ * @return Whethet the password matches
+ */
+ public static boolean ifPasswordMatches (final LoginContainer container, final User updatedUser) {
+ // Validate parameters
+ if (null == container) {
+ // Throw NPE
+ throw new NullPointerException("container is null");
+ } else if (null == updatedUser) {
+ // And again NPE ...
+ throw new NullPointerException("updatedUser is null");
+ } else if (container.getUser() == null) {
+ // NPE for user in container
+ throw new NullPointerException("container.user is null");
+ } else if (container.getUserPassword() == null) {
+ // NPE for user password in container
+ throw new NullPointerException("container.userPassword is null");
+ } else if (container.getUserPassword().isEmpty()) {
+ // Empty password in container
+ throw new IllegalArgumentException("container.userPassword is empty");
+ }
+
+ // First encrypt password
+ String encryptedPassword = Crypt.crypt(container.getUserPassword(), updatedUser.getUserEncryptedPassword());
+
+ // Is it matching?
+ return encryptedPassword.equals(updatedUser.getUserEncryptedPassword());
+ }
+
/**
* No instance from this class
*/