mirror of
https://github.com/koloideal/Quizzi.git
synced 2026-06-10 18:35:28 +03:00
commit
This commit is contained in:
@@ -10,6 +10,12 @@ class BotConfig:
|
||||
creator_id: int
|
||||
|
||||
|
||||
@dataclass
|
||||
class SecurityConfig:
|
||||
test_hash_salt: str
|
||||
test_hash_length: int = 16
|
||||
|
||||
|
||||
@dataclass
|
||||
class DatabaseConfig:
|
||||
host: str
|
||||
@@ -27,6 +33,7 @@ class DatabaseConfig:
|
||||
class Config:
|
||||
bot: BotConfig
|
||||
database: DatabaseConfig
|
||||
security: SecurityConfig
|
||||
|
||||
@classmethod
|
||||
def from_toml(cls, path: str | Path) -> Self:
|
||||
@@ -35,6 +42,7 @@ class Config:
|
||||
|
||||
bot_data: dict[str, str | int] = data["bot"]
|
||||
db_data: dict[str, str | int] = data["database"]
|
||||
security_data: dict[str, str | int] = data["security"]
|
||||
|
||||
return cls(
|
||||
bot=BotConfig(
|
||||
@@ -47,5 +55,9 @@ class Config:
|
||||
user=str(db_data["user"]),
|
||||
password=str(db_data["password"]),
|
||||
database=str(db_data["database"])
|
||||
),
|
||||
security=SecurityConfig(
|
||||
test_hash_salt=str(security_data["test_hash_salt"]),
|
||||
test_hash_length=int(security_data.get("test_hash_length", 16))
|
||||
)
|
||||
)
|
||||
|
||||
@@ -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]
|
||||
Reference in New Issue
Block a user