|
@@ -1,14 +1,36 @@
|
|
|
package thyyxxk.webserver.utils;
|
|
|
|
|
|
-import java.text.DateFormat;
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Random;
|
|
|
|
|
|
public class Test {
|
|
|
+ public static final String[] SOURCE = {"abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "0123456789", "!@#$%^&*()_+,.<>/?{}[]"};
|
|
|
+
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
|
|
+ List<Integer> usedSourceIndexes = new ArrayList<>();
|
|
|
+ int passwordLength = 12;
|
|
|
+ String password;
|
|
|
+ for (int i = 0; i < passwordLength; i++) {
|
|
|
+ do {
|
|
|
+ password = generatePassword(usedSourceIndexes, passwordLength);
|
|
|
+ } while (usedSourceIndexes.size() < SOURCE.length);
|
|
|
+ System.out.println(password);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String generatePassword(List<Integer> usedSourceIndexes, int passwordLength) {
|
|
|
+ usedSourceIndexes.clear();
|
|
|
+ StringBuilder passwordBuilder = new StringBuilder();
|
|
|
+ for (int i = 0; i < passwordLength; i++) {
|
|
|
+ int sourceIndex = new Random().nextInt(SOURCE.length);
|
|
|
+ if (!usedSourceIndexes.contains(sourceIndex)) {
|
|
|
+ usedSourceIndexes.add(sourceIndex);
|
|
|
+ }
|
|
|
+ int sourceItemIndex = new Random().nextInt(SOURCE[sourceIndex].length());
|
|
|
+ passwordBuilder.append(SOURCE[sourceIndex].split("")[sourceItemIndex]);
|
|
|
+ }
|
|
|
+ return passwordBuilder.toString();
|
|
|
}
|
|
|
}
|