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
|
public static String generatePassword(){
Stream<Character> pwdStream = Stream.concat(getRandomNumbers(2),
Stream.concat(getRandomSpecialChars(2),
Stream.concat(getRandomAlphabets(2, true), getRandomAlphabets(4, false))));
List<Character> charList = pwdStream.collect(Collectors.toList());
Collections.shuffle(charList);
String password = charList.stream()
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
.toString();
return password;
}
private static Stream<Character> getRandomNumbers(int count) {
return getRandomChars(count, 48,57);
}
private static Stream<Character> getRandomAlphabets(int count, boolean lowerCase) {
return getRandomChars(count,lowerCase? 65:97,lowerCase?90:122);
}
private static Stream<Character> getRandomChars(int count, int origin, int bound) {
Random random = new SecureRandom();
IntStream specialChars = random.ints(count, origin, bound);
return specialChars.mapToObj(data -> (char) data);
}
public static Stream<Character> getRandomSpecialChars(int count) {
return getRandomChars(count, 33, 45);
}
|