Advertisement
To6oKukata

Untitled

Jan 22nd, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. 1. import java.util.Random;
  2. 2.
  3. 3. public class PasswordGenerator {
  4. 4.
  5. 5. private static final String CAPITAL_LETTERS =
  6. 6.
  7. 7. "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  8. 8.
  9. 9. private static final String SMALL_LETTERS =
  10. 10.
  11. 11. "abcdefghijklmnopqrstuvwxyz";
  12. 12.
  13. 13. private static final String DIGITS = "0123456789";
  14. 14.
  15. 15. private static final String SPECIAL_CHARS =
  16. 16.
  17. 17. "~!@#$%^&*()_+=`{}[]\\|':;.,/?<>";
  18. 18.
  19. 19. private static final String ALL_CHARS =
  20. 20.
  21. 21. CAPITAL_LETTERS + SMALL_LETTERS + DIGITS + SPECIAL_CHARS;
  22. 22.
  23. 23. private static Random rnd = new Random();
  24. 24.
  25. 25. public static void main(String[] args) {
  26. 26.
  27. 27. StringBuilder password = new StringBuilder();
  28. 28.
  29. 29. for (int i=1; i<=2; i++) {
  30. 30.
  31. 31. char capitalLetter = generateChar(CAPITAL_LETTERS);
  32. 32.
  33. 33. insertAtRandomPosition(password, capitalLetter);
  34. 34.
  35. 35. }
  36. 36.
  37. 37. for (int i=1; i<=2; i++) {
  38. 38.
  39. 39. char smallLetter = generateChar(SMALL_LETTERS);
  40. 40.
  41. 41. insertAtRandomPosition(password, smallLetter);
  42. 42.
  43. 43. }
  44. 44.
  45. 45. char digit = generateChar(DIGITS);
  46. 46.
  47. 47. insertAtRandomPosition(password, digit);
  48. 48.
  49. 49. for (int i=1; i<=3; i++) {
  50. 50.
  51. 51. char specialChar = generateChar(SPECIAL_CHARS);
  52. 52.
  53. 53. insertAtRandomPosition(password, specialChar);
  54. 54.
  55. 55. }
  56. 56.
  57. 57. int count = rnd.nextInt(8);
  58. 58.
  59. 59. for (int i=1; i<=count; i++) {
  60. 60.
  61. 61. char specialChar = generateChar(ALL_CHARS);
  62. 62.
  63. 63. insertAtRandomPosition(password, specialChar);
  64. 64.
  65. 65. }
  66. 66.
  67. 67.
  68. 68.
  69. 69. System.out.println(password);
  70. 70.
  71. 71. }
  72. 72.
  73. 73.
  74. 74.
  75. 75. private static void insertAtRandomPosition(
  76. 76.
  77. 77. StringBuilder password, char character) {
  78. 78.
  79. 79. int randomPosition = rnd.nextInt(password.length()+1);
  80. 80.
  81. 81. password.insert(randomPosition, character);
  82. 82.
  83. 83. }
  84. 84.
  85. 85.
  86. 86.
  87. 87. private static char generateChar(String availableChars) {
  88. 88.
  89. 89. int randomIndex = rnd.nextInt(availableChars.length());
  90. 90.
  91. 91. char randomChar = availableChars.charAt(randomIndex);
  92. 92.
  93. 93. return randomChar;
  94. 94.
  95. 95.
  96. 96. }
  97. 97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement