UUID, UID, SecureRandom or MessageDigest? Number of ways to Create Unique ID/Key in Java

Java UUID, UID, Message Digest and Random Number Example

Is your Enterprise project related to User Transaction? Is your Java Project processes money transaction? Is your Java Project involves DB transaction? Are you running multithreaded application?

Well, in that case you must have created Unique transaction ID in your project.

There are number of ways you could generate Unique Keys/IDs in Java. Java provides some utilities for us to generate those unique identifier.

Let’s take a look at number of ways we could create Unique Keys in Java

  1. java.util.UUID: cryptographically strong pseudo random number generator. Introduced in Java 5
  2. java.rmi.server.UID: unique ID over time with respect to the host that it was generated on
  3. java.security.SecureRandom: provides a cryptographically strong random number generator using instance NativePRNG or SHA1PRNG
  4. java.security.MessageDigest: provides applications the functionality of a message digest algorithm, such as SHA-1 or SHA-256

Let’s take a look at Example:

Output:

Have anything to add to this article? Please chime in and join the conversion.