18 lines
454 B
Python
18 lines
454 B
Python
"""Tokens, Prüfsummen und Vergleiche für Sitzungen und CSRF."""
|
|
|
|
import hashlib
|
|
import hmac
|
|
import secrets
|
|
|
|
|
|
def new_token() -> str:
|
|
return secrets.token_urlsafe(32)
|
|
|
|
|
|
def token_hash(token: str) -> str:
|
|
"""Sitzungstokens sind 256 Bit Zufall; SHA-256 genügt und erlaubt einen indizierten Lookup."""
|
|
return hashlib.sha256(token.encode()).hexdigest()
|
|
|
|
|
|
def same(a: str, b: str) -> bool:
|
|
return hmac.compare_digest(a.encode(), b.encode())
|