🚸 Generate consistent aliases using hashes

This commit is contained in:
Rayan fernandes
2021-06-06 17:18:17 +05:30
parent dc5988ce8d
commit 80da16a880

View File

@@ -1,6 +1,10 @@
import base64
import hashlib
import random import random
import string import string
from ..config import SHORT_URL_LEN
from .home_view import HomeView from .home_view import HomeView
from .wildcard_view import WildcardView from .wildcard_view import WildcardView
from .download import Download from .download import Download
@@ -13,6 +17,7 @@ from .logout_view import LogoutView
from .middlewhere import middleware_factory from .middlewhere import middleware_factory
class Views( class Views(
HomeView, HomeView,
Download, Download,
@@ -33,12 +38,15 @@ class Views(
chat_id = chat.id chat_id = chat.id
title = chat.title title = chat.title
while True: while True:
alias_id = "".join( # alias_id = "".join(
[ # [
random.choice(string.ascii_letters + string.digits) # random.choice(string.ascii_letters + string.digits)
for _ in range(len(str(chat_id))) # for _ in range(len(str(chat_id)))
] # ]
) # )
orig_id = f"{title}{chat_id}" # the original id
alias_id = base64.urlsafe_b64encode(hashlib.md5(orig_id.encode()).digest())[:SHORT_URL_LEN].decode()
if alias_id in self.chat_ids: if alias_id in self.chat_ids:
continue continue