Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. package com.software.berater.test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /**
  7. * Created by bileser on 06.10.15.
  8. */
  9. public class RandomPasswordGenerator {
  10.  
  11. private String newPassword = "";
  12. private static final String DIGITS = "123456789";
  13. private static final String LOCASE_CHARACTERS = "abcdefghjkmnpqrstuvwxyz";
  14. private static final String UPCASE_CHARACTERS = "ABCDEFGHJKMNPQRSTUVWXYZ";
  15. private static final String SYMBOLS = "-_;:()%&$§";
  16. private static java.util.Random r = new java.util.Random();
  17.  
  18. public String getNewPassword(int maxLength) {
  19. if (maxLength < 4){
  20. maxLength = 4;
  21. }
  22. List<String> pools = new ArrayList<String>();
  23. pools.add(RandomPasswordGenerator.DIGITS);
  24. pools.add(RandomPasswordGenerator.LOCASE_CHARACTERS);
  25. pools.add(RandomPasswordGenerator.UPCASE_CHARACTERS);
  26. pools.add(RandomPasswordGenerator.SYMBOLS);
  27. StringBuilder sb = new StringBuilder();
  28.  
  29. while (sb.length() != maxLength){
  30. for (String pool : pools){
  31. boolean takeChar = false;
  32. char charCandidate = 'a';
  33. while (!takeChar){
  34. char[] charPool = pool.toCharArray();
  35. charCandidate = charPool[r.nextInt(charPool.length)];
  36. if (charCandidate == 'l' && sb.toString().contains("1")){
  37. } else if (charCandidate == 'O' && sb.toString().contains("0")){
  38. } else {
  39. takeChar = true;
  40. }
  41. }
  42. sb.append(charCandidate);
  43. if (sb.length() == maxLength){
  44. return sb.toString();
  45. }
  46. }
  47. }
  48. return null;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement