feat(auth): required login barrier with 1-click demo access and session management
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
import hashlib
|
||||
import os
|
||||
import secrets
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
salt = secrets.token_hex(16)
|
||||
key = hashlib.pbkdf2_hmac(
|
||||
'sha256',
|
||||
password.encode('utf-8'),
|
||||
salt.encode('utf-8'),
|
||||
100000
|
||||
)
|
||||
return f"{salt}${key.hex()}"
|
||||
|
||||
def verify_password(password: str, hashed: str) -> bool:
|
||||
try:
|
||||
salt, key_hex = hashed.split('$')
|
||||
computed_key = hashlib.pbkdf2_hmac(
|
||||
'sha256',
|
||||
password.encode('utf-8'),
|
||||
salt.encode('utf-8'),
|
||||
100000
|
||||
)
|
||||
return secrets.compare_digest(computed_key.hex(), key_hex)
|
||||
except Exception:
|
||||
return False
|
||||
Reference in New Issue
Block a user