Play uuid from java code in python

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

UUID.namedUUIDFromBytesdoesn't take a namespace as a parameter, whereas in python uuid.uuid3does. According to What namespace does the JDK use to generate UUIDs with the name UUIDFromBytes? , the namespace should be passed as part of the name, but it is no longer possible to change the java code.

Is there a way to create my own namespace in python code so that it yields the same UUID as Java code?

+4
source share
2 answers

nameUUIDFromBytestakes only one parameter, which should be a concatenation of the namespace and name just like you say. The namespace parameter should be UUID, and as far as I know, they do not matter null.

A " nulluuid" can be passed to Python uuid3like this. This should work as long as the namespace has an attribute bytes(checked with Python 2 and 3):

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

In case this is useful, if you want to do this part of Java, you can use the following:

UUID namespaceUUID = UUID.fromString("9db60607-6b12-41eb-8848-eafd26681583");
String myString = "sometextinhere";

ByteBuffer buffer = ByteBuffer.wrap(new byte[16 + myString.getBytes().length]);
buffer.putLong(namespaceUUID.getMostSignificantBits());
buffer.putLong(namespaceUUID.getLeastSignificantBits());
buffer.put(myString.getBytes());

byte[] uuidBytes = buffer.array();

UUID myUUID = UUID.nameUUIDFromBytes(uuidBytes);

This will provide the same output UUID as the following Python:

namespaceUUID = UUID('9db60607-6b12-41eb-8848-eafd26681583')
myUUID = uuid.uuid3(myUUID, 'sometextinhere'))
+2
source

All Articles