add hashing functionality #14

Open
ouraniath wants to merge 1 commit from ouraniath/2025-plovdiv-git:hash into live_coding
Showing only changes of commit 9c71173dc3 - Show all commits

View file

@ -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,10 +9,20 @@ 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}: ')
password = getpass.getpass(f'Enter password for {username}: ')
password_hashed = hash_pass(password)
pwdb[username] = password_hashed
return pwdb
def read_pwdb(pwdb_path):