Skip to content

Instantly share code, notes, and snippets.

@cedricvidal
Created February 11, 2024 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cedricvidal/6115567e32da2303644852863833290a to your computer and use it in GitHub Desktop.
Save cedricvidal/6115567e32da2303644852863833290a to your computer and use it in GitHub Desktop.
Python UUID5 using SHA-256
# 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