java password

Solutions on MaxInterview for java password by the best coders in the world

showing results for - "java password"
Van
21 Oct 2020
1 public static String generateRandomPassword(int length) {
2        Object[][] characterSets = {
3                {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'},
4                {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'},
5                {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
6        };
7        List<Character> passwordCharacters = new ArrayList<>();
8        ThreadLocalRandom random = ThreadLocalRandom.current();
9        for (Object[] characters : characterSets){
10            for (int i = 0; i < length / 3; i++){
11                passwordCharacters.add((Character) characters[random.nextInt(0, characters.length)]);
12            }
13        }
14        for (int i =0; i < length % 3; i++){
15            Object[] characters = characterSets[random.nextInt(0, characterSets.length)];
16            passwordCharacters.add((Character) characters[random.nextInt(0, characters.length)]);
17        }
18        Collections.shuffle(passwordCharacters);
19        StringBuilder stringBuilder = new StringBuilder(length);
20        passwordCharacters.forEach(stringBuilder::append);
21        return stringBuilder.toString();
22    }