Hashes the provided password using the password hasher.
By default the hashed password is converted to hexadecimal format for
storage. This ensures that the hashed password can be safely stored in
a database or other storage medium. If you prefer to store the hashed
password in its original format, you can set the convert_to_hex
parameter to False.
| Parameters: |
-
password
(str | None)
–
The password to be hashed.
-
convert_to_hex
(bool, default:
True
)
–
A boolean flag indicating whether to convert the hashed password to
hexadecimal format. Defaults to True.
|
| Returns: |
-
str
–
The hashed password as a hexadecimal string.
|
Source code in src/alpha/factories/password_factory.py
| def hash_password(
self, password: str | None, convert_to_hex: bool = True
) -> str:
"""Hashes the provided password using the password hasher.
By default the hashed password is converted to hexadecimal format for
storage. This ensures that the hashed password can be safely stored in
a database or other storage medium. If you prefer to store the hashed
password in its original format, you can set the `convert_to_hex`
parameter to False.
Parameters
----------
password
The password to be hashed.
convert_to_hex
A boolean flag indicating whether to convert the hashed password to
hexadecimal format. Defaults to True.
Returns
-------
str
The hashed password as a hexadecimal string.
Raises
------
exceptions.WrongPasswordException
Raised when the provided password is None or empty.
"""
if not password:
raise exceptions.WrongPasswordException("Password value is empty")
hashed_password = self._password_hasher.hash(password=password)
return (
self._to_hex(hashed_password)
if convert_to_hex
else hashed_password
)
|