Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pgdp.iter;
- import java.nio.charset.Charset;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Random;
- public class PasswordIterator implements Iterator<String> {
- private static int passwordLength;
- public PasswordIterator(int passwordLength) {
- PasswordIterator.passwordLength = passwordLength;
- if (passwordLength < 1 || passwordLength > 9)
- Util.badArgument("Passwortlänge liegt im Intervall 1 bis 9");
- }
- public ArrayList<String> iterator(ArrayList<String> matched) {
- ArrayList<String> result = new ArrayList<String>();
- // Code1 that consists of repeating the same digit
- for (int i = 0; i < 10; i++) {
- Iterator<String> code1 = (Iterator<String>) new Range(i, i);
- while (result.size() <= passwordLength) {
- result.add(code1.next());
- }
- }
- // Code2 that represents an ascending sequence of digits
- for (int i = 0; i < 9; i++) {
- Iterator<String> code2 = (Iterator<String>) new Range(i, i + 1);
- while (result.size() <= passwordLength) {
- result.add(code2.next());
- }
- }
- // Codes that represents a descending sequence of digits
- for (int i = 9; i > 0; i--) {
- Iterator<String> code3 = (Iterator<String>) new Range(i, i - 1);
- while (result.size() <= passwordLength) {
- result.add(code3.next());
- }
- }
- matched = result; // mark the string result as matched
- // All other codes of the given length
- byte[] code4 = new byte[passwordLength];
- new Random().nextBytes(code4);
- String resultOfRandom = new String(code4, Charset.forName("UTF-8"));
- if (!isMatched(resultOfRandom))
- result.add(resultOfRandom);
- return result;
- }
- public static boolean isMatched(String result) {
- String[] matched = new String[100000];
- for (int i = 0; i < matched.length; i++) {
- if (matched[i].contains(result))
- return true;
- }
- return false;
- }
- @Override
- public boolean hasNext() {
- // TODO Auto-generated method stub
- return false;
- }
- @Override
- public String next() {
- // TODO Auto-generated method stub
- return null;
- }
- public static void main(String[] args) {
- PasswordIterator iter = new PasswordIterator(4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement