Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package com.progressoft.jip8;
  2.  
  3. import java.security.SecureRandom;
  4. import java.util.Arrays;
  5. import java.util.Collections;
  6. import java.util.List;
  7.  
  8. public class PasswordGenerator {
  9. public String generate() {
  10. return generateRandomPassword(8);
  11. }
  12.  
  13. static String chars = "abcdefghijklmnopqrstuvwxyz";
  14. static String nums = "0123456789";
  15. static String sChars = "_$@%";
  16.  
  17. static String password = chars + nums + sChars;
  18. static String password_shuffle = shuffleString(password);
  19. static String ok_password = password_shuffle;
  20.  
  21. static SecureRandom random = new SecureRandom();
  22.  
  23. public static void main(String[] args) {
  24. System.out.println("password : " + generateRandomPassword(8));
  25. System.out.println("\n");
  26. }
  27.  
  28. public static String generateRandomPassword(int length) {
  29. if (length < 1) throw new IllegalArgumentException();
  30. int count_chars = 0, count_num = 0, count_schar = 0;
  31. StringBuilder sb = new StringBuilder(length);
  32. for (int i = 0; i < length;) {
  33. int rndCharAt = random.nextInt(ok_password.length());
  34. char rndChar = ok_password.charAt(rndCharAt);
  35. if (chars.contains(rndChar + "") && count_chars <4){
  36. sb.append(rndChar);
  37. count_chars++;
  38. i++;
  39. }
  40. if (nums.contains(rndChar + "") && count_num <2){
  41. sb.append(rndChar);
  42. count_num++;
  43. i++;
  44. }
  45. if (sChars.contains(rndChar + "") && count_schar <2){
  46. sb.append(rndChar);
  47. count_schar++;
  48. i++;
  49. }
  50. }
  51. return sb.toString();
  52. }
  53.  
  54.  
  55. public static String shuffleString(String string)
  56. {
  57. List<String> letters = Arrays.asList(string.split(""));
  58. Collections.shuffle(letters);
  59. String shuffled = "";
  60. for (String letter : letters) {
  61. shuffled += letter;
  62. }
  63. return shuffled;
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement