forked from mtusi2026/mtusi2026lab5
ЛР5 — БВТ2302 — Никитина — на 5
This commit is contained in:
@@ -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 бита энтропии
|
||||
Reference in New Issue
Block a user