How to generate a random unique string with more than 2^30 combination. I also wanted to reverse the process. Is this possible?

Posted by Yusuf S on Stack Overflow See other posts from Stack Overflow or by Yusuf S
Published on 2011-03-14T01:53:59Z Indexed on 2011/03/19 0:10 UTC
Read the original article Hit count: 234

Filed under:
|
|
|
|

I have a string which contains 3 elements:

  • a 3 digit code (example: SIN, ABD, SMS, etc)
  • a 1 digit code type (example: 1, 2, 3, etc)
  • a 3 digit number (example: 500, 123, 345)

Example string: SIN1500, ABD2123, SMS3345, etc..

I wanted to generate a UNIQUE 10 digit alphanumeric and case sensitive string (only 0-9/a-z/A-Z is allowed), with more than 2^30 (about 1 billion) unique combination per string supplied. The generated code must have a particular algorithm so that I can reverse the process. For example:

public static void main(String[] args) {
    String test = "ABD2123";
    String result = generateData(test);
    System.out.println(generateOutput(test)); //for example, the output of this is: 1jS8g4GDn0
    System.out.println(generateOutput(result)); //the output of this will be ABD2123 (the original string supplied)
}

What I wanted to ask is is there any ideas/examples/libraries in java that can do this? Or at least any hint on what keyword should I put on Google?

I tried googling using the keyword java checksum, rng, security, random number, etc and also tried looking at some random number solution (java SecureRandom, xorshift RNG, java.util.zip's checksum, etc) but I can't seem to find one?

Thanks!

EDIT:

My use case for this program is to generate some kind of unique voucher number to be used by specific customers.

The string supplied will contains 3 digit code for company ID, 1 digit code for voucher type, and a 3 digit number for the voucher nominal. I also tried adding 3 random alphanumeric (so the final digit is 7 + 3 digit = 10 digit). This is what I've done so far, but the result is not very good (only about 100 thousand combination):

public static String in ="somerandomstrings";
public static String out="someotherrandomstrings";

public static String encrypt(String kata)
throws Exception {
    String result="";
    String ina=in;
    String outa=out;
    Random ran = new Random();
    Integer modulus=in.length();
    Integer offset= ((Integer.parseInt(Utils.convertDateToString(new Date(), "SS")))+ran.nextInt(60))/2%modulus;

    result=ina.substring(offset, offset+1);
    ina=ina+ina;
    ina=ina.substring(offset, offset+modulus);
    result=result+translate(kata, ina, outa);

    return result;
}

EDIT:

I'm sorry I forgot to put the "translate" function :

    public static String translate(String kata,String  seq1, String  seq2){
    String result="";
    if(kata!=null&seq1!=null&seq2!=null){
        String[] a=kata.split("");
        for (int j = 1; j < a.length; j++) {
            String b=a[j];                      
            String[]seq1split=seq1.split("");
            String[]seq2split=seq2.split("");
            int hint=seq1.indexOf(b)+1;

            String sq="";
            if(seq1split.length>hint)
                sq=seq1split[hint];
            String sq1="";
            if(seq2split.length>hint)
                sq1=seq2split[hint];

            b=b.replace(sq, sq1);

            result=result+b;
        }
    }
    return result;
}

© Stack Overflow or respective owner

Related posts about java

Related posts about security