31 lines
825 B
Python
31 lines
825 B
Python
import secrets
|
|
|
|
|
|
class MicroUUID:
|
|
def __init__(self):
|
|
self.uuid = self.generate_uuid()
|
|
|
|
def generate_uuid(self):
|
|
# Generate a random UUID (version 4)
|
|
random_bytes = secrets.token_bytes(16)
|
|
# Set the version to 4 (randomly generated UUID)
|
|
random_bytes = bytearray(random_bytes)
|
|
random_bytes[6] = (random_bytes[6] & 0x0F) | 0x40
|
|
# Set the variant to RFC 4122
|
|
random_bytes[8] = (random_bytes[8] & 0x3F) | 0x80
|
|
uid = "".join(f"{byte:02x}" for byte in random_bytes)
|
|
return f"{uid[0:8]}-{uid[8:12]}-{uid[12:16]}-{uid[16:20]}-{uid[20:32]}"
|
|
|
|
def __str__(self):
|
|
return self.uuid
|
|
|
|
|
|
# Example usage:
|
|
def example():
|
|
uuid_instance = MicroUUID()
|
|
print(f"Generated UUID: {uuid_instance}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
example()
|