File: base.py

package info (click to toggle)
python-pwdlib 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 632 kB
  • sloc: python: 403; makefile: 14
file content (38 lines) | stat: -rw-r--r-- 1,128 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import typing


def validate_str_or_bytes(value: typing.Any, param_name: str) -> None:
    """
    Validate that a value is a string or bytes.

    Args:
        value: The value to validate.
        param_name: The name of the parameter being validated.

    Raises:
        TypeError: If the value is not a string or bytes.
    """
    if not isinstance(value, (str, bytes)):
        raise TypeError(f"{param_name} must be str or bytes")  # noqa: TRY003


def ensure_str(v: str | bytes, *, encoding: str = "utf-8") -> str:
    return v.decode(encoding) if isinstance(v, bytes) else v


def ensure_bytes(v: str | bytes, *, encoding: str = "utf-8") -> bytes:
    return v.encode(encoding) if isinstance(v, str) else v


class HasherProtocol(typing.Protocol):
    @classmethod
    def identify(cls, hash: str | bytes) -> bool: ...

    def hash(self, password: str | bytes, *, salt: bytes | None = None) -> str: ...

    def verify(self, password: str | bytes, hash: str | bytes) -> bool: ...

    def check_needs_rehash(self, hash: str | bytes) -> bool: ...


__all__ = ["HasherProtocol", "ensure_str", "validate_str_or_bytes"]