added hashing for a password

This commit is contained in:
ASPP Student 2025-09-22 16:12:59 +03:00
parent 33469b8840
commit a9277e682b

View file

@ -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)