Reputation: 391
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
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
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