A Python port of the Invisible Internet Project (I2P)
at main 28 lines 861 B view raw
1"""Authentication — optional password protection for the console. 2 3Provides password hashing and verification for the console's 4optional authentication feature. 5 6Ported from net.i2p.router.web.ConsolePasswordManager. 7""" 8 9from __future__ import annotations 10 11import hashlib 12import secrets 13 14 15def generate_password_hash(password: str) -> str: 16 """Generate a salted SHA-256 hash of a password.""" 17 salt = secrets.token_hex(16) 18 h = hashlib.sha256((salt + password).encode("utf-8")).hexdigest() 19 return f"{salt}:{h}" 20 21 22def check_password(password: str, password_hash: str) -> bool: 23 """Check a password against a salted hash.""" 24 if ":" not in password_hash: 25 return False 26 salt, expected = password_hash.split(":", 1) 27 h = hashlib.sha256((salt + password).encode("utf-8")).hexdigest() 28 return secrets.compare_digest(h, expected)