From 9ad5cab717f61a369d4c994e3f0239114c9e6ffa Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Mon, 22 Sep 2025 16:08:01 +0300 Subject: [PATCH 1/2] add password hashing --- minimal_auth.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/minimal_auth.py b/minimal_auth.py index 9f535a7..e3d96e2 100644 --- a/minimal_auth.py +++ b/minimal_auth.py @@ -1,6 +1,7 @@ import getpass # hides types characters, very useful import json import sys +import hashlib # python standard lib for hashing def get_credentials(): username = input('Enter your username: ') @@ -11,7 +12,9 @@ def authenticate(username, password, pwdb): return password == pwdb[username] def add_user(username, pwdb): - pwdb[username] = getpass.getpass(f'Enter password for {username}: ') + password = getpass.getpass(f'Enter password for {username}: ') + # hash the password before saving to the database + pwdb[username] = hashlib.sha256(f'{password}'.encode()).hexdigest() return pwdb def read_pwdb(pwdb_path): -- 2.39.5 From 9350b0e82a8329714e3dd99501072b8a677287c1 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Mon, 22 Sep 2025 16:20:57 +0300 Subject: [PATCH 2/2] update authentication with hashed password --- minimal_auth.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/minimal_auth.py b/minimal_auth.py index e3d96e2..e97b350 100644 --- a/minimal_auth.py +++ b/minimal_auth.py @@ -8,13 +8,20 @@ def get_credentials(): password = getpass.getpass('Enter your password: ') return (username, password) +# add a function that hashes the password +def hash_password(password): + return hashlib.sha256(f'{password}'.encode()).hexdigest() + def authenticate(username, password, pwdb): - return password == pwdb[username] + correct_password = pwdb[username] + # add this line to hash the entered password to then compared with the stored password + attempted_password = hash_password(password) + return correct_password == attempted_password def add_user(username, pwdb): password = getpass.getpass(f'Enter password for {username}: ') # hash the password before saving to the database - pwdb[username] = hashlib.sha256(f'{password}'.encode()).hexdigest() + pwdb[username] = hash_password(password) return pwdb def read_pwdb(pwdb_path): -- 2.39.5