0

How to get same string generated by pseudo random generator with some initialized string in java. I want something like this

I get this code from somewhere. It produces fine random string but what I want is that whenever I pass some string let suppose "Name" with it, it gives me the same random string generated with this function. Means I want same random string with "Name".

public static String RandomAlphaNumericString(int numChars)
{
    char[] All = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456879".toCharArray();
    SecureRandom srand = new SecureRandom();
    Random rand = new Random();
    char[] buff = new char[numChars];
    for (int i = 0; i < numChars; ++i) {
        // reseed rand once you've used up all available entropy bits
        if ((i % 10) == 0) {
          rand.setSeed(srand.nextLong()); // 64 bits of random!
        }
        buff[i] = All[rand.nextInt(All.length)];
    }
    return new String(buff);
}
Muhammad Noman
  • 1,344
  • 1
  • 14
  • 28

1 Answers1

0

You don't want a random string then if you want predictable results based on input. What you want is a hash if you want the same encoded key returned each time. If you also want to decrypt the encoded key, then you want encryption rather than hashing. Try researching how to hash a string, e.g. see this previous stackoverflow question.

Community
  • 1
  • 1
Kip
  • 560
  • 7
  • 16