Search code examples
javapythonuuid

Reproduce uuid from java code in python


In a Java application files are created where the filename is a UUID generated from a protein sequence (e.g. TTCCPSIVARSNFNVCRLPGTPEAICATYTGCIIIPGATCPGDYAN) created using the function UUID.nameUUIDFromBytes. This results in the UUID c6a0deb5-0c4f-3961-9d19-3f0fde0517c2.

UUID.namedUUIDFromBytes doesn't take a namespace as a parameter, whereas in python uuid.uuid3 does. According to What namespace does the JDK use to generate a UUID with nameUUIDFromBytes?, the namespace should have been passed as part of the name, but it's no longer possible to change the java code.

Is there a way to create a custom namespace in the python code such that it will produce the same UUID's as the Java code?


Solution

  • nameUUIDFromBytes only takes one parameter, which is supposed to be the concatenation of the namespace and name just like you say. The namespace parameter is supposed to be a UUID, and as far as I know, they don't have a null value defined.

    A "null uuid" can be passed to Python's uuid3 like this. This should work as long as the namespace has a bytes attribute (tested with Python 2 and 3):

    class NULL_NAMESPACE:
        bytes = b''
    uuid.uuid3(NULL_NAMESPACE, 'TTCCPSIVARSNFNVCRLPGTPEAICATYTGCIIIPGATCPGDYAN')
    # returns: UUID('c6a0deb5-0c4f-3961-9d19-3f0fde0517c2')