From 9c71173dc3ced4d44d8ef0b6f2ca2ea6afea69b3 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Mon, 22 Sep 2025 16:21:54 +0300 Subject: [PATCH] add hashing functionality --- minimal_auth.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/minimal_auth.py b/minimal_auth.py index 9f535a7..9bfdb06 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 #base python lib to hash strings def get_credentials(): username = input('Enter your username: ') @@ -8,11 +9,21 @@ def get_credentials(): return (username, password) def authenticate(username, password, pwdb): - return password == pwdb[username] + return hash_pass(password) == pwdb[username] + +def hash_pass(text_password): + ''' + hashes the user's string input to sha256 + ''' + password_in_bytes = bytearray(text_password, 'utf-8') + password_hashed = hashlib.sha256(password_in_bytes).hexdigest() + return password_hashed def add_user(username, pwdb): - pwdb[username] = getpass.getpass(f'Enter password for {username}: ') - return pwdb + password = getpass.getpass(f'Enter password for {username}: ') + password_hashed = hash_pass(password) + pwdb[username] = password_hashed + return pwdb def read_pwdb(pwdb_path): try: -- 2.39.5