Rapportsystem API - of D03N
Hovedprosjekt FiV Programmering 20-24
Loading...
Searching...
No Matches
pw_manager.py
Go to the documentation of this file.
1import bcrypt
2
3#Defines the PasswordManager class
5 def __init__(self):
6 pass
7
8 #Hashes a plain password and return its hashed value.
9 def encrypt_pw(self, plaintext_pw: str) -> bytes:
10
11 #convert the plaintext password string to bytes
12 password_bytes = plaintext_pw.encode('utf-8')
13
14 #Hashes the password
15 hashed_password = bcrypt.hashpw(password_bytes, bcrypt.gensalt())
16 return hashed_password
17
18 #checks if password is correct
19 def check_pw(self, plaintext_pw: str, stored_hashed_password: str) -> bool:
20 # Check if a password matches the hashed password
21 # Convert the plaintext password string to bytes
22 password_bytes = plaintext_pw.encode('utf-8')
23 stored_hashed_password_bytes = stored_hashed_password.encode('utf-8')
24
25 # Verify the password
26 return bcrypt.checkpw(password_bytes, stored_hashed_password_bytes)
27
28
29#defines hash, takes password and returns hashed password
30def hash(password):
31 password_manager = PwManager()
32 hashed_pw = password_manager.encrypt_pw(password)
33 return hashed_pw
34
35#defines check, takes password and hashed password and returns true/false
36def check(password, hashed_pw):
37 password_manager = PwManager()
38 is_password_correct = password_manager.check_pw(password, hashed_pw)
39 print("Password correct: ", is_password_correct)
40 return is_password_correct
bytes encrypt_pw(self, str plaintext_pw)
Definition pw_manager.py:9
bool check_pw(self, str plaintext_pw, str stored_hashed_password)
Definition pw_manager.py:19
check(password, hashed_pw)
Definition pw_manager.py:36
hash(password)
Definition pw_manager.py:30