File size: 577 Bytes
0f4b0ea |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import hashlib
import string
def generate_id(text: str) -> str:
"""Generate a 3-character alphanumeric hash from a URL that is unlikely to collide."""
hash_object = hashlib.md5(text.encode())
hash_hex = hash_object.hexdigest()
# Convert to integer
hash_int = int(hash_hex, 16)
# Convert to base62 using the same character set
characters = string.ascii_lowercase + string.digits
base = len(characters)
result = ""
for _ in range(3):
result = characters[hash_int % base] + result
hash_int //= base
return result
|