Advertisement
Alice_Kim

Untitled

Dec 14th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. package pgdp.iter;
  2.  
  3. import java.nio.charset.Charset;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.Random;
  7.  
  8. public class PasswordIterator implements Iterator<String> {
  9. private static int passwordLength;
  10.  
  11. public PasswordIterator(int passwordLength) {
  12. PasswordIterator.passwordLength = passwordLength;
  13. if (passwordLength < 1 || passwordLength > 9)
  14. Util.badArgument("Passwortlänge liegt im Intervall 1 bis 9");
  15. }
  16.  
  17. public ArrayList<String> iterator(ArrayList<String> matched) {
  18. ArrayList<String> result = new ArrayList<String>();
  19. // Code1 that consists of repeating the same digit
  20. for (int i = 0; i < 10; i++) {
  21. Iterator<String> code1 = (Iterator<String>) new Range(i, i);
  22. while (result.size() <= passwordLength) {
  23. result.add(code1.next());
  24. }
  25. }
  26. // Code2 that represents an ascending sequence of digits
  27. for (int i = 0; i < 9; i++) {
  28. Iterator<String> code2 = (Iterator<String>) new Range(i, i + 1);
  29. while (result.size() <= passwordLength) {
  30. result.add(code2.next());
  31. }
  32. }
  33. // Codes that represents a descending sequence of digits
  34. for (int i = 9; i > 0; i--) {
  35. Iterator<String> code3 = (Iterator<String>) new Range(i, i - 1);
  36. while (result.size() <= passwordLength) {
  37. result.add(code3.next());
  38. }
  39. }
  40. matched = result; // mark the string result as matched
  41. // All other codes of the given length
  42. byte[] code4 = new byte[passwordLength];
  43. new Random().nextBytes(code4);
  44. String resultOfRandom = new String(code4, Charset.forName("UTF-8"));
  45. if (!isMatched(resultOfRandom))
  46. result.add(resultOfRandom);
  47. return result;
  48. }
  49.  
  50. public static boolean isMatched(String result) {
  51. String[] matched = new String[100000];
  52. for (int i = 0; i < matched.length; i++) {
  53. if (matched[i].contains(result))
  54. return true;
  55. }
  56. return false;
  57. }
  58.  
  59. @Override
  60. public boolean hasNext() {
  61. // TODO Auto-generated method stub
  62. return false;
  63. }
  64.  
  65. @Override
  66. public String next() {
  67. // TODO Auto-generated method stub
  68. return null;
  69. }
  70.  
  71. public static void main(String[] args) {
  72. PasswordIterator iter = new PasswordIterator(4);
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement