From 413b7db8a480ea6e0d3e8c9729502282ca748a84 Mon Sep 17 00:00:00 2001 From: MonadChains Date: Fri, 24 Mar 2023 00:42:43 +0100 Subject: 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. --- Lib/uuid.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'Lib/uuid.py') diff --git a/Lib/uuid.py b/Lib/uuid.py index 1c5578bf1f..698be34873 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) -- cgit v1.2.1