Skip to content
Open
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
29 changes: 29 additions & 0 deletions uuid_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import uuid

def generate_uuid() -> str:
"""
Generate a universally unique identifier (UUID) using a cryptographically secure random number generator.

Returns:
str: A UUID following RFC 4122 version 4 format.
"""
return str(uuid.uuid4())

# Test the generated UUID to ensure it meets the criteria
def test_uuid_format() -> None:
uuid_str = generate_uuid()
assert len(uuid_str) == 36, "UUID should be a 36-character string"
assert "-" in uuid_str, "UUID should have correct hyphen placement"
for char in uuid_str:
assert char in "0123456789abcdefABCDEF-", "UUID should contain only hexadecimal characters and hyphens"

# Ensure two consecutive UUID generations produce different values
def test_consecutive_uuids() -> None:
uuid1 = generate_uuid()
uuid2 = generate_uuid()
assert uuid1 != uuid2, "Two consecutive UUID generations should produce different values"

# Run the test functions
if __name__ == "__main__":
test_uuid_format()
test_consecutive_uuids()