ЛР5 — БВТ2302 — Никитина — на 5

This commit is contained in:
Yuliiiaaaa
2026-11-24 13:22:27 +03:00
parent 276dbdad8a
commit 067ee4eb52
35 changed files with 1493 additions and 52 deletions
+16
View File
@@ -0,0 +1,16 @@
import hashlib, os, secrets
def hash_password(password: str) -> str:
salt = secrets.token_bytes(16)
key = hashlib.scrypt(password.encode(), salt=salt, n=16384, r=8, p=1, dklen=64)
return salt.hex() + "$" + key.hex()
def verify_password(password: str, stored: str) -> bool:
salt_hex, key_hex = stored.split("$")
salt = bytes.fromhex(salt_hex)
expected = bytes.fromhex(key_hex)
actual = hashlib.scrypt(password.encode(), salt=salt, n=16384, r=8, p=1, dklen=64)
return secrets.compare_digest(actual, expected)
def generate_token() -> str:
return secrets.token_urlsafe(48) # ~384 бита энтропии