diff options
author | MonadChains <monadchains@gmail.com> | 2023-03-23 23:42:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-23 23:42:43 (GMT) |
commit | 413b7db8a480ea6e0d3e8c9729502282ca748a84 (patch) | |
tree | 605e3b2e78cddf41558d7e0b0f010496f33cceee /Lib/uuid.py | |
parent | f1e3eeebc01941f9c25bca4d2fa6313cf5041cc4 (diff) | |
download | cpython-413b7db8a480ea6e0d3e8c9729502282ca748a84.zip cpython-413b7db8a480ea6e0d3e8c9729502282ca748a84.tar.gz cpython-413b7db8a480ea6e0d3e8c9729502282ca748a84.tar.bz2 |
gh-94684: uuid: support bytes in the name argument to uuid3/5 (#94709)
RFC 4122 does not specify that name should be a string, so for completness the functions should also support a name given as a raw byte sequence.
Diffstat (limited to 'Lib/uuid.py')
-rw-r--r-- | Lib/uuid.py | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/Lib/uuid.py b/Lib/uuid.py index 1c5578b..698be34 100644 --- a/Lib/uuid.py +++ b/Lib/uuid.py @@ -711,9 +711,11 @@ def uuid1(node=None, clock_seq=None): def uuid3(namespace, name): """Generate a UUID from the MD5 hash of a namespace UUID and a name.""" + if isinstance(name, str): + name = bytes(name, "utf-8") from hashlib import md5 digest = md5( - namespace.bytes + bytes(name, "utf-8"), + namespace.bytes + name, usedforsecurity=False ).digest() return UUID(bytes=digest[:16], version=3) @@ -724,8 +726,10 @@ def uuid4(): def uuid5(namespace, name): """Generate a UUID from the SHA-1 hash of a namespace UUID and a name.""" + if isinstance(name, str): + name = bytes(name, "utf-8") from hashlib import sha1 - hash = sha1(namespace.bytes + bytes(name, "utf-8")).digest() + hash = sha1(namespace.bytes + name).digest() return UUID(bytes=hash[:16], version=5) |