added hashing for a password #11

Open
georgiido wants to merge 1 commit from georgiido/2025-plovdiv-git:hashpw into live_coding
Showing only changes of commit a9277e682b - Show all commits

View file

@ -1,6 +1,7 @@
import getpass # hides types characters, very useful import getpass # hides types characters, very useful
import json import json
import sys import sys
import hashlib # library for hashing
def get_credentials(): def get_credentials():
username = input('Enter your username: ') username = input('Enter your username: ')
@ -8,10 +9,10 @@ def get_credentials():
return (username, password) return (username, password)
def authenticate(username, password, pwdb): def authenticate(username, password, pwdb):
return password == pwdb[username] return pwhash(password) == pwdb[username]
def add_user(username, pwdb): 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 return pwdb
def read_pwdb(pwdb_path): def read_pwdb(pwdb_path):
@ -26,6 +27,12 @@ def write_pwdb(pwdb, pwdb_path):
pwdb_file = open(pwdb_path, 'wt') pwdb_file = open(pwdb_path, 'wt')
json.dump(pwdb, pwdb_file) json.dump(pwdb, pwdb_file)
def pwhash(password):
# encodes password with sha256
return hashlib.sha256(password.encode()).hexdigest()
pwdb_path = 'pwdb.json' pwdb_path = 'pwdb.json'
pwdb = read_pwdb(pwdb_path) pwdb = read_pwdb(pwdb_path)