Created
February 11, 2024 18:38
-
-
Save cedricvidal/6115567e32da2303644852863833290a to your computer and use it in GitHub Desktop.
Python UUID5 using SHA-256
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate a UUID5 from a name using SHA-256 | |
def uuid5_sha256(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 uuid import UUID | |
import hashlib | |
hash = hashlib.sha256(namespace.bytes + name).digest() | |
return UUID(bytes=hash[:16], version=5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment