elna / app /tools /utils.py
David Chu
feat: reference citations by a short id
0f4b0ea unverified
raw
history blame contribute delete
577 Bytes
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