Skip to content

[2.7] bpo-34145: Fix uuid3 and uuid5 to accept unicode names #10100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Lib/test/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ def test_uuid3(self):
'dd1a1cef-13d5-368a-ad82-eca71acd4cd1'),
(uuid.uuid3(uuid.NAMESPACE_X500, 'c=ca'),
'658d3002-db6b-3040-a1d1-8ddd7d189a4d'),
(uuid.uuid3(uuid.NAMESPACE_X500, name=u'foo'),
'c3c8dbcd-8f60-3825-b42b-162fc311726f'),
]:
equal(u.variant, uuid.RFC_4122)
equal(u.version, 3)
Expand Down Expand Up @@ -401,6 +403,8 @@ def test_uuid5(self):
'1447fa61-5277-5fef-a9b3-fbc6e44f4af3'),
(uuid.uuid5(uuid.NAMESPACE_X500, 'c=ca'),
'cc957dd1-a972-5349-98cd-874190002798'),
(uuid.uuid5(uuid.NAMESPACE_X500, name=u'foo'),
'a2dfac7d-e8aa-556e-b751-a37169330d23'),
]:
equal(u.variant, uuid.RFC_4122)
equal(u.version, 5)
Expand Down
2 changes: 2 additions & 0 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ 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."""
from hashlib import md5
name = name.encode('utf-8')
hash = md5(namespace.bytes + name).digest()
return UUID(bytes=hash[:16], version=3)

Expand All @@ -606,6 +607,7 @@ def uuid4():
def uuid5(namespace, name):
"""Generate a UUID from the SHA-1 hash of a namespace UUID and a name."""
from hashlib import sha1
name = name.encode('utf-8')
hash = sha1(namespace.bytes + name).digest()
return UUID(bytes=hash[:16], version=5)

Expand Down