Random identifier in Java

I want to generate random identifiers in Java The identifier should have a fixed size and the probability of generating the same identifier twice should be very low (the system has about 500000 users) The identifier should be so long that it is not feasible to "guess it" by brute force attack

So far, my method is as follows:

String alphabet = "0123456789ABCDE....and so on";
int lengthOfAlphabet = 42; 
long length = 12; 

public String generateIdentifier(){
    String identifier = "";
    Random random = new Random(); 
    for(int i = 0;i<length;i++){
        identifier+= alphabet.charAt(random.nextInt(lengthOfAlphabet));
    }
    return identifier; 
}

I enforce uniqueness through constraints in the database If I click on an identifier that has been created, I will continue to generate until an unused identifier is found

My assumption is that I can adjust lengthofalphabet and length to get the attribute I'm looking for:

>Rare collisions > brute force infeasible > the identifier should be as short as possible because the system user must enter the identifier

Is this a good way? Does anyone have any idea about the value of "length"?

Solution

I think random UUID is your friend It is a fixed width http://docs.oracle.com/javase/1.5.0/docs/api/java/util/UUID.html#randomUUID ()

If I remember my math correctly, because UUID is 32 hexadecimal digits (0-F), the number of permutations is 16 ^ 32, which is a large number, so it is difficult to guess

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>