This commit is contained in:
2026-01-03 02:12:28 +03:00
parent 9e822789d2
commit 8273ede069
12 changed files with 285 additions and 25 deletions
@@ -0,0 +1,25 @@
import hashlib
import hmac
import string
def generate_alpha_id(n: int, secret_key: str, length: int = 16) -> str:
data = str(n).encode('utf-8')
key = secret_key.encode('utf-8')
digest = hmac.new(key, data, hashlib.sha256).digest()
num = int.from_bytes(digest, byteorder='big')
alphabet = string.ascii_letters
result = []
while num > 0:
num, rem = divmod(num, 52)
result.append(alphabet[rem])
encoded = "".join(result)
if len(encoded) < length:
encoded = encoded.ljust(length, alphabet[0])
return encoded[:length]