I want to generate UUIDs based on objects. Objects that are equal need to have the same UUID.
I read about the type 3 UUIDs whose value is based on a name and namespace. java.util.UUID
has a nameUUIDFromBytes
method that takes a byte array as argument.
So I was thinking of serializing my objects into byte arrays and feeding those to the nameUUIDFromBytes
method.
But I am confused about that namespace aspect of the UUID. Does that mean the UUID will be different when generated on another machine?
What is the best way to generate UUIDs such that when obj1.equals(obj2) == true
, then uuid1.equals(uuid2) == true
even when uuid1 is generated on another machine than uuid2?
EDIT: to those who voted this as a duplicate of how to implement a hashCode, please reopen this. I am asking about UUIDs, not hashCodes. Unless you think the best way to generate UUIDs is to use a hashCode. If so, please write an answer as to why that is the best way instead of closing this question with something that hardly has anything to do with it.
The UUID that an operating system will generate reserves the right to blend in information from the machine along with the time information &c. (In fact early Microsoft UUID generators would take network card information which was really quite insecure since it was possible to back that out from a generated UUID!).
So using your favourite UUID generator is not appropriate.
What you could do is to essentially enhance the methods used to create a hash code, extending that to 128 bits. Convert that byte array to a UUID format, and you're done.