Commit 34b3ac53 authored by 技术部-任文超's avatar 技术部-任文超

更新随机密码生成器

parent 9f069e62
...@@ -22,7 +22,9 @@ public class PasswordUtil { ...@@ -22,7 +22,9 @@ public class PasswordUtil {
private static final char[] PWD_BASE = { private static final char[] PWD_BASE = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':', ';',
'<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'
}; };
public final static String MD5WithSalt(String s){ public final static String MD5WithSalt(String s){
...@@ -63,16 +65,11 @@ public class PasswordUtil { ...@@ -63,16 +65,11 @@ public class PasswordUtil {
*/ */
public static String generateRandomPwd(int pwdLen) { public static String generateRandomPwd(int pwdLen) {
Preconditions.checkArgument(pwdLen > 0); Preconditions.checkArgument(pwdLen > 0);
int pwdMax = PWD_BASE.length;
int i; // 生成的随机数
int count = 0; // 生成的密码的长度
StringBuilder pwd = new StringBuilder(); StringBuilder pwd = new StringBuilder();
while (count < pwdLen) { while (pwd.length() < pwdLen) {
i = ThreadLocalRandom.current().nextInt(pwdMax); // 生成的数最大为36-1 // 随机整数区间: [0, PWD_BASE.length - 1)
if (i >= 0 && i < PWD_BASE.length) { int index = ThreadLocalRandom.current().nextInt(0, PWD_BASE.length);
pwd.append(PWD_BASE[i]); pwd.append(PWD_BASE[index]);
count++;
}
} }
return pwd.toString(); return pwd.toString();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment