Coverage for src/lexigram/admin/lib/password.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-21 14:56 +0800

1"""Password hashing helpers for admin user management.""" 

2 

3from __future__ import annotations 

4 

5__all__ = ["hash_password"] 

6 

7 

8def hash_password(plain: str) -> str: 

9 """Hash a plain-text password using bcrypt. 

10 

11 Bcrypt with 12 rounds is used. A missing ``bcrypt`` package raises 

12 ``RuntimeError`` instead of degrading to an unsalted digest. 

13 

14 Args: 

15 plain: Plain-text password string. 

16 

17 Returns: 

18 Hashed password string suitable for storage. 

19 

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 

29 

30 hashed = bcrypt.hashpw(plain.encode("utf-8"), bcrypt.gensalt(rounds=12)) 

31 return hashed.decode("utf-8")