*/
package org.mxchange.jusercore.model.user;
+import java.security.SecureRandom;
+import java.text.MessageFormat;
+import java.util.Random;
import org.apache.commons.codec.digest.Crypt;
import org.mxchange.jcore.BaseFrameworkSystem;
import org.mxchange.jusercore.container.login.LoginContainer;
*/
public class UserUtils extends BaseFrameworkSystem {
+ /**
+ * Password alphabet
+ */
+ private static final String PASSWORD_ALPHABET = "abcdefghijklmnopqrstuvwxzyABCDEFGHIJKLMNOPQRSTUVWXZY0123456789-/?!_+#@";
+
+ /**
+ * Minimum password length
+ */
+ private static final Integer PASSWORD_MINIMUM_LENGTH = 5;
+
+ /**
+ * Random number generator
+ */
+ private static final Random RANDOM_NUMBER_GENERATOR;
+
+ /**
+ * Static initializer
+ */
+ static {
+ // Init RNG
+ RANDOM_NUMBER_GENERATOR = new SecureRandom();
+ }
+
+ /**
+ * Creates a pseudo-random password with given length
+ * <p>
+ * @param length Length of the password
+ * <p>
+ * @return Pseudo-random password
+ */
+ public static String createRandomPassword (final Integer length) {
+ // Parameter should be valid
+ if (null == length) {
+ // Throw NPE
+ throw new NullPointerException("length is null");
+ } else if (length < PASSWORD_MINIMUM_LENGTH) {
+ // To weak passwords
+ throw new IllegalArgumentException(MessageFormat.format("Password length {0} is to short, minimum: {1}", length, PASSWORD_MINIMUM_LENGTH));
+ }
+
+ // Init variable
+ String password = "";
+
+ // Start creating it
+ for (int i = 0; i < length; i++) {
+ // Generate random number
+ int pos = RANDOM_NUMBER_GENERATOR.nextInt(PASSWORD_ALPHABET.length());
+
+ // Get char at this position and add it to the final password
+ String dummy = password.concat(String.valueOf(PASSWORD_ALPHABET.charAt(pos)));
+ }
+
+ // Should have the wanted length
+ assert (password.length() == length) : "Password length " + password.length() + " doesn't match requested: " + length;
+
+ // Return it
+ return password;
+ }
+
/**
* Hashes given user password and adds a salt to it
* <p>