-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordGenerator.java
More file actions
93 lines (85 loc) · 3.41 KB
/
PasswordGenerator.java
File metadata and controls
93 lines (85 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.java;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
/**
* 描述:动态自动生成随机复杂密码
* @Author: mzy
* @Date: 2019-8-20 11:15
*/
public class PasswordGenerator {public static final char[] allowedSpecialCharactors = {
'`', '~', '@', '#', '$', '%', '^', '&',
'*', '(', ')', '-', '_', '=', '+', '[',
'{', '}', ']', '\\', '|', ';', ':', '"',
'\'', ',', '<', '.', '>', '/', '?'};//密码能包含的特殊字符
private static final int letterRange = 26;
private static final int numberRange = 10;
private static final int spCharactorRange = allowedSpecialCharactors.length;
private static final Random random = new Random();
private int passwordLength;//密码的长度
private int minVariousType;//密码包含字符的最少种类
public PasswordGenerator(int passwordLength, int minVariousType) {
if (minVariousType > CharactorType.values().length) minVariousType = CharactorType.values().length;
if (minVariousType > passwordLength) minVariousType = passwordLength;
this.passwordLength = passwordLength;
this.minVariousType = minVariousType;
}
public String generateRandomPassword() {
char[] password = new char[passwordLength];
List<Integer> pwCharsIndex = new ArrayList();
for (int i = 0; i < password.length; i++) {
pwCharsIndex.add(i);
}
List<CharactorType> takeTypes = new ArrayList(Arrays.asList(CharactorType.values()));
List<CharactorType> fixedTypes = Arrays.asList(CharactorType.values());
int typeCount = 0;
while (pwCharsIndex.size() > 0) {
int pwIndex = pwCharsIndex.remove(random.nextInt(pwCharsIndex.size()));//随机填充一位密码
Character c;
if (typeCount < minVariousType) {//生成不同种类字符
c = generateCharacter(takeTypes.remove(random.nextInt(takeTypes.size())));
typeCount++;
} else {//随机生成所有种类密码
c = generateCharacter(fixedTypes.get(random.nextInt(fixedTypes.size())));
}
password[pwIndex] = c.charValue();
}
return String.valueOf(password);
}
private Character generateCharacter(CharactorType type) {
Character c = null;
int rand;
switch (type) {
case LOWERCASE://随机小写字母
rand = random.nextInt(letterRange);
rand += 97;
c = new Character((char) rand);
break;
case UPPERCASE://随机大写字母
rand = random.nextInt(letterRange);
rand += 65;
c = new Character((char) rand);
break;
case NUMBER://随机数字
rand = random.nextInt(numberRange);
rand += 48;
c = new Character((char) rand);
break;
case SPECIAL_CHARACTOR://随机特殊字符
rand = random.nextInt(spCharactorRange);
c = new Character(allowedSpecialCharactors[rand]);
break;
}
return c;
}
public static void main(String[] args) {
System.out.println(new PasswordGenerator(10, 3).generateRandomPassword());
}
}
enum CharactorType {
LOWERCASE,
UPPERCASE,
NUMBER,
SPECIAL_CHARACTOR
}