Cat Feeder  1.0.0
The Cat feeder project
Loading...
Searching...
No Matches
password_handling.py
Go to the documentation of this file.
1r"""
2# +==== BEGIN CatFeeder =================+
3# LOGO:
4# ..............(..../\
5# ...............)..(.')
6# ..............(../..)
7# ...............\‍(__)|
8# Inspired by Joan Stark
9# source https://www.asciiart.eu/
10# animals/cats
11# /STOP
12# PROJECT: CatFeeder
13# FILE: password_handling.py
14# CREATION DATE: 11-10-2025
15# LAST Modified: 14:53:9 19-12-2025
16# DESCRIPTION:
17# This is the backend server in charge of making the actual website work.
18# /STOP
19# COPYRIGHT: (c) Cat Feeder
20# PURPOSE: File containing the class in charge of handling password for the server.
21# // AR
22# +==== END CatFeeder =================+
23"""
24
25import bcrypt
26from typing import Union
27from display_tty import Disp, initialise_logger
28
29
31 """__summary__
32 """
33
34 disp: Disp = initialise_logger(__qualname__, False)
35
36 def __init__(self, error: int = 84, success: int = 0, debug: bool = False) -> None:
37 # ------------------------ The logging function ------------------------
38 self.disp.update_disp_debug(debug)
39 self.disp.log_debug("Initialising...")
40 # -------------------------- Inherited values --------------------------
41 self.debug: bool = debug
42 self.success: int = success
43 self.error: int = error
44 self.salt_rounds = 10
45 self.disp.log_debug("Initialised")
46
47 def hash_password(self, password: Union[str, bytes]) -> str:
48 """
49 The function to hash the password for the security
50 Args:
51 password (str): The entered password
52
53 Returns:
54 str: The hashed password
55
56 Raises:
57 TypeError: if the provided password does not match the expected type.
58 """
59 title = "_hash_password"
60 self.disp.log_debug("Enter hash password", f"{title}")
61 if not isinstance(password, (str, bytes)):
62 raise TypeError("The password is neither bytes nor a string")
63 if isinstance(password, str):
64 password = bytes(password, encoding="utf-8")
65 self.disp.log_debug("Start register endpoint", f"{title}")
66 salt = bcrypt.gensalt(rounds=self.salt_rounds)
67 safe_password = bcrypt.hashpw(password, salt)
68 return safe_password.decode("utf-8")
69
70 def check_password(self, password: Union[str, bytes], password_hash: bytes) -> bool:
71 """
72 The function to check the entered password with the hashed password
73 Args:
74 password (str): The entered password
75 password_hash (bytes): The hashed password
76
77 Returns:
78 bool: True if it's the same, False if not
79
80 Raises:
81 TypeError: if the provided password does not match the expected type.
82 """
83 msg = f"password = {type(password)}, "
84 msg += f"password_hash = {type(password_hash)}"
85 self.disp.log_debug(msg, "check_password")
86 if not isinstance(password, (str, bytes)):
87 raise TypeError("The password is neither bytes nor a string")
88 if not isinstance(password_hash, (str, bytes)):
89 raise TypeError("The password_hash is neither bytes nor a string")
90 if isinstance(password, str):
91 password = password.encode("utf-8")
92 if isinstance(password_hash, str):
93 password_hash = password_hash.encode("utf-8")
94 msg = f"password = {type(password)}, password_hash = "
95 msg += f"{type(password_hash)}"
96 self.disp.log_debug(msg, "check_password")
97 return bcrypt.checkpw(password, password_hash)
bool check_password(self, Union[str, bytes] password, bytes password_hash)
None __init__(self, int error=84, int success=0, bool debug=False)