Question

I have gone to this site many times and found answers to my questions but its finally time for me to post one of my own! So the objective of a particular class in my software is to generate random passwords of fixed length, comprised of 'low' ASCII characters. The main catch is that I do not want to generate the same password twice but always guarantee uniqueness. Initially I used a HashMap in order to hash each password I had generated so far and use as a check each time I created a new one before returning. However, Java HashMap objects are limited in size and eventually the Map would become too saturated to maintain acceptable retrieval time. The following is my latest crack at the problem:

package gen;

import java.util.Set;

import java.util.Random;

import java.util.HashSet;

public class Generator {

Random r;
int length;
Set<String> seen;

public Generator(int l){
    seen = new HashSet<String>();
    length = l;
    r = new Random();
    r.setSeed(System.currentTimeMillis());
}

public String generate(){   
    String retval = "";
    int i = 0;
    while(i<length){
        int rand = r.nextInt(93)+33;
        if(rand!=96){
            retval+= (char)rand;
            i++;
        }
    }
    return retval;
}

public String generateNoRepeat(){
    String retval;
    int i;
    do{
        retval ="";
        i = 0;
        while(i<length){
            int rand = r.nextInt(93)+33;
            if(rand!=96){
                retval+= (char)rand;
                i++;
            }
        }
    }while(!seen.add(retval));
    return retval;
}
}

Edit: Thanks so much for the Set suggestion. It makes my code so much cleaner now too!

I may decide to just use the dumb generator method to fill up a BlockingQueue and just multithread it to death...

Further clarification: This is not meant to generate secure passwords. It must simply guarantee that it will eventually generate all possible passwords and only once for a given length and character set.

Note:

I have taken everyone's insight and have come to the conclusion that sequentially generating the possible passwords and storing them to the disk is probably my best option. Either that or simply allow duplicate passwords and supplement the inefficiency with multiple Generator threads.

Was it helpful?

Solution

Why not just encrypt sequential numbers?

Let n be the first number in your sequence (don't start with zero). Let e be some encryption algorithm (e.g. RSA).

Then your passwords are e(n), e(n+1), e(n+2), ...

But I heavily agree with Greg Hewgill and Ted Hopp, avoiding duplicates is more trouble than it is worth.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top