views:

25

answers:

3

Hello,

I am having problems taking in a string (a persons name), and getting back a unique integer, it keeps going through the catch function, and I dont know why, other than the way I wrote SecureRandom is not working, but it is very confusing. I am very new to programming so please be kind!

  public static int uinqueID(String name){
    try{
      SecureRandom srA = SecureRandom.getInstance(name);
      Integer randomA = new Integer(srA.nextInt());
      System.out.println(randomA);
      UUID uuidA = UUID.randomUUID();
      String randomNum2 = uuidA.toString();
      System.out.println(randomNum2);
      int randomB = Integer.valueOf(randomNum2);
      int uniqueID = randomA + randomB;
      return uniqueID;
    } catch(NoSuchAlgorithmException e) {
      System.err.println("I failed");
    }
    return -1;
  }

The output I am getting is: I failed -1

Thank you for your help!

+1  A: 

You cannot use the name of the person to get a SecureRandom object. It expects the name of in implementation of a random number generator. You can use "SHA1PRNG" as that is default available. You can then seed the random generator with the name.getBytes() and then get the next random number.

From the Javadoc:

public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException Generates a SecureRandom object that ...snip...

Parameters: algorithm - the name of the PRNG algorithm. See Appendix A in the Java Cryptography Architecture API Specification & Reference for information about standard PRNG algorithm names.

Returns: the new SecureRandom object. Throws: NoSuchAlgorithmException - if the PRNG algorithm is not available in the caller's environment. Since: 1.2

You can skip the rest because the hash is as good as the algorithm and adding other things to it hardly make it more secure.

something like :

static int getUUID(String name) throws NoSuchAlgorithmException {
    SecureRandom srA = SecureRandom.getInstance("SHA1PRNG");
    srA.setSeed(name.getBytes());
    return new Integer(srA.nextInt());
}

I would have normally used a MessageDigest, but I must admit this is pretty tight.

Peter Tillemans
ok! This makes more sense, so then can I use the name as a seed in a byte array?
Jack
yep, I added an example.
Peter Tillemans
A: 

According to the docs, NoSuchAlgorithmException is thrown

...when a particular cryptographic algorithm is requested but is not available in the environment.

You need to specify a Random Number Generator (RNG) algorithm to getInstance(String algorithm), not a person's name.

If you just need a random number, try replacing that line in your code with

SecureRandom srA = new SecureRandom();

If you don't care how random the number is and you're just trying to create a unique integer for the name, then I think all you'll need is to call hashCode on the name.

Bill the Lizard
A: 

If you want to map a String to a number, you need a hash function, or a digest. Take a look at java.security.MessageDigest

gawi