From a9277e682b435f4eac01385ef7a8f64580489a2f Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Mon, 22 Sep 2025 16:12:59 +0300 Subject: [PATCH] added hashing for a password --- minimal_auth.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/minimal_auth.py b/minimal_auth.py index 9f535a7..032bf65 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 # library for hashing def get_credentials(): username = input('Enter your username: ') @@ -8,10 +9,10 @@ def get_credentials(): return (username, password) def authenticate(username, password, pwdb): - return password == pwdb[username] + return pwhash(password) == pwdb[username] def add_user(username, pwdb): - pwdb[username] = getpass.getpass(f'Enter password for {username}: ') + pwdb[username] = pwhash(getpass.getpass(f'Enter password for {username}: ')) return pwdb def read_pwdb(pwdb_path): @@ -26,6 +27,12 @@ def write_pwdb(pwdb, pwdb_path): pwdb_file = open(pwdb_path, 'wt') json.dump(pwdb, pwdb_file) +def pwhash(password): + # encodes password with sha256 + return hashlib.sha256(password.encode()).hexdigest() + + + pwdb_path = 'pwdb.json' pwdb = read_pwdb(pwdb_path) -- 2.39.5