Guy Kobrinsky
Guy Kobrinsky

Reputation: 391

Easiest way in Java to turn String into UUID

How to generate a valid UUID from a String? The String alone is not what I'm looking for. Rather, I'm looking for something like a hash function converting any String to a valid UUID.

Upvotes: 39

Views: 90369

Answers (2)

Raj
Raj

Reputation: 4089

Try this out:

String superSecretId = "f000aa01-0451-4000-b000-000000000000";
UUID.fromString(superSecretId);

I am using this in my project and it works. Make sure you import the right stuff.

Upvotes: 37

Chris Martin
Chris Martin

Reputation: 30756

In the Java core library, there's java.util.UUID.nameUUIDFromBytes(byte[]).

I wouldn't recommend it because it's UUID 3 which is based on MD5, a very broken hash function. You'd be better off finding an implementation of UUID 5, which is based on SHA-1 (better although also sort of broken).

Upvotes: 18

Related Questions