I need Long Security ID

Hi, I need to generate unique long ID for each client which will be transported from server to client and vise versa . As I see, J2SE has the next classes which may be useful to me: Random and SecureRandom.

What and why is better to use? Maybe it is better use another class or maybe external library? May you give me an advise. May you also give a link to specific article if you know. Thank you in advance.

[422 byte] By [dbes@isd.dp.uaa] at [2007-11-26 20:05:04]
# 1
java.util.UUID.randomUUID();
jverda at 2007-7-9 23:05:52 > top of Java-index,Java Essentials,Java Programming...
# 2
> java.util.UUID.randomUUID();Sorry, I forgot to say that I use jdk 1.4
dbes@isd.dp.uaa at 2007-7-9 23:05:52 > top of Java-index,Java Essentials,Java Programming...
# 3
Hey, do we have here security experts?
dbes@isd.dp.uaa at 2007-7-9 23:05:52 > top of Java-index,Java Essentials,Java Programming...
# 4

Is there a security issue? If so, use SecureRandom.nextLong(). Otherwise is there anything wrong with sequential IDs? It's a lot simpler:

static long nextID = 0;

public static synchronized getNextID()

{

return nextID++; // or ++nextID

}

ejpa at 2007-7-9 23:05:52 > top of Java-index,Java Essentials,Java Programming...
# 5
timestamp in milliseconds multiplied by the hashcode of the userid and then multiplied by the numeric representation of the ip address of the server should be unique enough.
jwentinga at 2007-7-9 23:05:52 > top of Java-index,Java Essentials,Java Programming...
# 6

> timestamp in milliseconds multiplied by the hashcode

> of the userid and then multiplied by the numeric

> representation of the ip address of the server should

> be unique enough.

Or he could just use SecureRandom, which is simple, compact, and more standard, and has more inputs.

jverda at 2007-7-9 23:05:53 > top of Java-index,Java Essentials,Java Programming...
# 7
and 'more unique', and more secure, ...
ejpa at 2007-7-9 23:05:53 > top of Java-index,Java Essentials,Java Programming...
# 8

> > timestamp in milliseconds multiplied by the

> hashcode

> > of the userid and then multiplied by the numeric

> > representation of the ip address of the server

> should

> > be unique enough.

>

> Or he could just use SecureRandom, which is simple,

> compact, and more standard, and has more inputs.

And how unique would those be in a clustered environment?

jwentinga at 2007-7-9 23:05:53 > top of Java-index,Java Essentials,Java Programming...
# 9

public class UniqueID {

static long current= System.currentTimeMillis();

static public synchronized long get(){

return current++;

}

}

java_2006a at 2007-7-9 23:05:53 > top of Java-index,Java Essentials,Java Programming...
# 10

> > > timestamp in milliseconds multiplied by the

> > hashcode

> > > of the userid and then multiplied by the numeric

> > > representation of the ip address of the server

> > should

> > > be unique enough.

> >

> > Or he could just use SecureRandom, which is

> simple,

> > compact, and more standard, and has more inputs.

>

> And how unique would those be in a clustered

> environment?

Probably at least as unique as the solution proposed above.

jverda at 2007-7-9 23:05:53 > top of Java-index,Java Essentials,Java Programming...
# 11

> public class UniqueID {

>static long current= System.currentTimeMillis();

> static public synchronized long get(){

>return current++;

> }

> }

As long as it doesn't have to be unique across hosts, JVM invocations, or multiple copies of that class loaded into the same VM, and as long as its okay for a client to guess other clients' IDs simply by adding to or subtracting from his own.

jverda at 2007-7-9 23:05:53 > top of Java-index,Java Essentials,Java Programming...
# 12

> > > > timestamp in milliseconds multiplied by the

> > > hashcode

> > > > of the userid and then multiplied by the

> numeric

> > > > representation of the ip address of the server

> > > should

> > > > be unique enough.

> > >

> > > Or he could just use SecureRandom, which is

> > simple,

> > > compact, and more standard, and has more inputs.

> >

> > And how unique would those be in a clustered

> > environment?

>

>

> Probably at least as unique as the solution proposed

> above.

why do you think I hashed the IP address of the server into the ID :)

jwentinga at 2007-7-9 23:05:53 > top of Java-index,Java Essentials,Java Programming...
# 13

http://java.sun.com/j2se/1.5.0/docs/api/java/security/SecureRandom.html:

'A cryptographically strong random number minimally complies with the statistical random number generator tests specified in FIPS 140-2, Security Requirements for Cryptographic Modules, section 4.9.1. Additionally, SecureRandom must produce non-deterministic output and therefore it is required that the seed material be unpredictable and that output of SecureRandom be cryptographically strong sequences as described in RFC 1750: Randomness Recommendations for Security.'

You could use the IP address, system time, workstation ID, process #, etc in the initial seed if you insist. But even without it, SecureRandom is by far the strongest solution available.

ejpa at 2007-7-9 23:05:53 > top of Java-index,Java Essentials,Java Programming...