]> git.mxchange.org Git - juser-core.git/commitdiff
Added method createRandomPassword() for creating pseudo-random passwords
authorRoland Haeder <roland@mxchange.org>
Mon, 11 Apr 2016 19:11:48 +0000 (21:11 +0200)
committerRoland Haeder <roland@mxchange.org>
Mon, 11 Apr 2016 19:11:48 +0000 (21:11 +0200)
src/org/mxchange/jusercore/model/user/UserUtils.java

index b98c7f9ef0b1c18912ce673f953a030369436c63..e7a299dd93087100f20ff3012e62fad501171de9 100644 (file)
@@ -16,6 +16,9 @@
  */
 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;
@@ -27,6 +30,65 @@ 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>