import hashlib from hashids import Hashids from django.conf import settings def hexdigest(text): text_hash = hashlib.sha256(text.encode()).hexdigest() return text_hash HASHIDS = Hashids(salt=settings.SECRET_KEY, min_length=12) # or 8 def encode_id(pk): return HASHIDS.encode(pk) def decode_slug(slug): return HASHIDS.decode(slug)[0] if HASHIDS.decode(slug) else None