added hashing for storing passwords #10

Open
pallavibe wants to merge 1 commit from pallavibe/2025-plovdiv-git:feat/create-hash into live_coding

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
from hashlib import sha256 # This library is used to create hashes out of the password
def get_credentials(): def get_credentials():
username = input('Enter your username: ') username = input('Enter your username: ')
@ -8,10 +9,12 @@ def get_credentials():
return (username, password) return (username, password)
def authenticate(username, password, pwdb): def authenticate(username, password, pwdb):
return password == pwdb[username] password_hash = sha256(password.encode("utf-8")).hexdigest()
return password_hash == pwdb[username]
def add_user(username, pwdb): def add_user(username, pwdb):
pwdb[username] = getpass.getpass(f'Enter password for {username}: ') pwd = getpass.getpass(f'Enter password for {username}: ')
pwdb[username] = sha256(pwd.encode("utf-8")).hexdigest()
return pwdb return pwdb
def read_pwdb(pwdb_path): def read_pwdb(pwdb_path):