I am trying to create a UUID that follows the RFC4122 requirements. The documentation for UUID v5 states:
uuid.uuid5(namespace, name)
Generate a UUID based on the SHA-1 hash of a namespace identifier (which is a UUID) and a name (which is a string).
and
uuid.RFC_4122
Specifies the UUID layout given in RFC 4122.
I do not understand what the "name" in the explanation above is:
>>> import uuid
>>> uuid.uuid5(uuid.RFC_4122, 'hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python310\lib\uuid.py", line 720, in uuid5
hash = sha1(namespace.bytes + bytes(name, "utf-8")).digest()
AttributeError: 'str' object has no attribute 'bytes'
It is surprisingly difficult to find an example of the generation of this UUID (I could not find any, anywhere).
The
uuidmodule is almost always following RFC 4122. That is, if you use one of theuuidxfunctions, it should give an RFC 4122 UUID.uuid.RFC_4122is not a namespace; it's just a string that says'specified in RFC 4122'. The namespaces are:NAMESPACE_DNSNAMESPACE_URLNAMESPACE_OIDNAMESPACE_X500To generate an RFC 4122 UUID, use one of these functions:
uuid1Generate a UUID from the time and hostname.
uuid3Generate a UUID from an MD5 hash of one of the namespaces above and a name.
uuid4Generate a random UUID.
uuid5Generate a UUID from an SHA1 hash of one of the namespaces above and a name.
It's preferred to use
uuid.uuid4(), because it's actually random.You can actually check that a UUID uses RFC 4122:
Note: for obvious reasons, you won't get the same UUID as me.