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!
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 :
I would have normally used a MessageDigest, but I must admit this is pretty tight.