Coverage for src/lexigram/admin/lib/password.py: 0%
9 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-24 23:18 +0800
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-24 23:18 +0800
1"""Password hashing helpers for admin user management."""
3from __future__ import annotations
5__all__ = ["hash_password"]
8def hash_password(plain: str) -> str:
9 """Hash a plain-text password using bcrypt.
11 Bcrypt with 12 rounds is used. A missing ``bcrypt`` package raises
12 ``RuntimeError`` instead of degrading to an unsalted digest.
14 Args:
15 plain: Plain-text password string.
17 Returns:
18 Hashed password string suitable for storage.
20 Raises:
21 RuntimeError: When the ``bcrypt`` package is not installed.
22 """
23 try:
24 import bcrypt
25 except ImportError as exc:
26 raise RuntimeError(
27 "bcrypt unavailable — refusing to hash admin passwords without a KDF"
28 ) from exc
30 hashed = bcrypt.hashpw(plain.encode("utf-8"), bcrypt.gensalt(rounds=12))
31 return hashed.decode("utf-8")