Created
November 4, 2025 04:03
-
-
Save HeshamMeneisi/a67d08e796c03bd5da5f7fe787b22a1a to your computer and use it in GitHub Desktop.
short_id_from_str (Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import base64 | |
| import hashlib | |
| BASE32_EXPANSION_FACTOR = 8 / 5 | |
| def short_id_from_str(s: str, max_len: int = 8) -> str: | |
| h = hashlib.blake2s( | |
| s.encode(), digest_size=int(max_len / BASE32_EXPANSION_FACTOR) | |
| ).digest() | |
| return base64.b32encode(h).decode().rstrip("=") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment