|
10 | 10 | * Creates a random password from ASCII letters
|
11 | 11 | * Given password length bounds
|
12 | 12 | *
|
13 |
| - * @author AKS1996 |
14 |
| - * @date 2017.10.25 |
| 13 | + * @author arrnavvv |
| 14 | + * @date 18-09-2020 |
15 | 15 | */
|
16 |
| -class PasswordGen { |
17 |
| - public static void main(String args[]) { |
18 |
| - String password = generatePassword(8, 16); |
19 |
| - System.out.print("Password: " + password); |
20 |
| - } |
21 |
| - |
22 |
| - static String generatePassword(int min_length, int max_length) { |
23 |
| - Random random = new Random(); |
24 |
| - |
25 |
| - String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
26 |
| - String lower = "abcdefghijklmnopqrstuvwxyz"; |
27 |
| - String numbers = "0123456789"; |
28 |
| - String specialChars = "!@#$%^&*(){}?"; |
| 16 | +public class PasswordGenerator { |
29 | 17 |
|
30 |
| - String allChars = upper + lower + numbers + specialChars; |
| 18 | + static String generatePassword(int min_length, int max_length){ |
| 19 | + String smallAlphabet="abcdefghijklmnopqrstuvwxyz"; |
| 20 | + String largeAlphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 21 | + String digits="1234567890"; |
| 22 | + String specialCharacters="!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; |
31 | 23 |
|
32 |
| - List<Character> letters = new ArrayList<Character>(); |
33 |
| - for (char c : allChars.toCharArray()) |
34 |
| - letters.add(c); |
| 24 | + String all = smallAlphabet+largeAlphabet+digits+specialCharacters; |
| 25 | + Random random = new Random(); |
| 26 | + int length = random.nextInt(max_length - min_length) + min_length; |
| 27 | + |
| 28 | + StringBuilder password = new StringBuilder(); |
| 29 | + password.append((smallAlphabet.charAt(random.nextInt(26)))). |
| 30 | + append(largeAlphabet.charAt(random.nextInt(26))). |
| 31 | + append(digits.charAt(random.nextInt(10))). |
| 32 | + append(specialCharacters.charAt(random.nextInt(31))); |
| 33 | + |
| 34 | + int left = length-4; |
| 35 | + StringBuilder remainingPassword = new StringBuilder(); |
| 36 | + while(left-->0){ |
| 37 | + remainingPassword.append(all.charAt(random.nextInt(93))); |
| 38 | + } |
| 39 | + password.append(remainingPassword); |
35 | 40 |
|
36 |
| - // Inbuilt method to randomly shuffle a elements of a list |
37 |
| - Collections.shuffle(letters); |
38 |
| - StringBuilder password = new StringBuilder(); |
| 41 | + ArrayList<Character> list = new ArrayList<>(); |
| 42 | + for (char c : password.toString().toCharArray()) |
| 43 | + list.add(c); |
| 44 | + Collections.shuffle(list); |
39 | 45 |
|
40 |
| - // Note that size of the password is also random |
41 |
| - for (int i = random.nextInt(max_length - min_length) + min_length; i > 0; --i) { |
42 |
| - password .append( letters.get(random.nextInt(letters.size()))); |
| 46 | + StringBuilder finalPassword = new StringBuilder(); |
| 47 | + for(int i=0;i<list.size();i++){ |
| 48 | + finalPassword.append(list.get(i)); |
43 | 49 | }
|
| 50 | + return finalPassword.toString(); |
| 51 | + } |
| 52 | + public static void main(String[] args) { |
44 | 53 |
|
45 |
| - return password.toString(); |
| 54 | + String password = generatePassword(8,16); |
| 55 | + System.out.println("Your password is: "+password); |
46 | 56 | }
|
47 | 57 | }
|
| 58 | + |
0 commit comments