Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What namespace does the JDK use to generate a UUID with nameUUIDFromBytes?

Tags:

java

uuid

The Sun/Oracle JDK exposes a function to create a type 3 (name based) UUID in the java.util package: java.util.UUID.nameUUIDFromBytes(byte[] name).

I need to be able to generate a type 3 UUID in Java using nameUUIDFromBytes and arrive at the same UUID when creating a type 3 UUID in another language, assuming I provide the same bytes as the source.

According to the javadocs this function creates a RFC 4122 compliant type 3 UUID. However, according to the RFC 4122 spec, a type 3 UUID must be created within some namespace. Most other languages allow you specify the namespace when creating a type 3 UUID (e.g. the UUIDTools gem in Ruby).

So my question is: what namespace UUID is used by the JDK when I invoke nameUUIDFromBytes?

like image 905
John McCann Avatar asked Feb 29 '12 18:02

John McCann


People also ask

How is Java UUID generated?

The UUIDs are generated using the hash of namespace and name. The namespace identifiers are UUIDs like Domain Name System (DNS), Object Identifiers (OIDs), URLs, etc. The only difference between UUIDv3 and UUIDv5 is the Hashing Algorithm — v3 uses MD5 (128 bits), while v5 uses SHA-1 (160 bits).

What is UUID randomUUID () in Java?

The randomUUID() method is used to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.

What is the datatype of UUID in Java?

A class that represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value. There exist different variants of these global identifiers.


1 Answers

See this bug report

Especially the comment, near the bottom:

Perhaps the course of action at this point would be to fix the javadoc stating "nameUUIDFromBytes(byte[] namespaceAndName) "one should pass-in a byte array containing the concatenation of the namespace UUID's bytes and the name bytes (in that order)" That's assuming the method just MD5's the byte[] and sets the fields as per the IETF document.

I don't know if i trust this to work correctly, but it should be easy to test using the predefined namespeces from the UUID spec, comparing with same UUID generated by some other implementation.

like image 140
theglauber Avatar answered Oct 15 '22 18:10

theglauber