api_keys.py 371 B

123456789101112131415161718
  1. import hashlib
  2. import secrets
  3. API_KEY_PREFIX = "agp"
  4. def generate_api_key() -> str:
  5. return f"{API_KEY_PREFIX}_{secrets.token_urlsafe(32)}"
  6. def hash_api_key(api_key: str) -> str:
  7. return hashlib.sha256(api_key.encode("utf-8")).hexdigest()
  8. def get_api_key_prefix(api_key: str) -> str:
  9. if len(api_key) <= 12:
  10. return api_key
  11. return api_key[:12]