implement hashing method using hashlib #16

Open
karlama wants to merge 1 commit from karlama/2024-heraklion-git:hashing-solution into main

12
auth.py
View file

@ -1,19 +1,27 @@
import json import json
import sys import sys
from getpass import getpass from getpass import getpass
import hashlib
PWDB_PATH = 'pwdb.json' PWDB_PATH = 'pwdb.json'
def hashing(password):
pwd_string = bytes(str(password), 'utf-8')
hash_pwd = hashlib.sha256(pwd_string).hexdigest()
return hash_pwd
def get_credentials(): def get_credentials():
username = input('Enter your username: ') username = input('Enter your username: ')
password = getpass('Enter your password: ') password = getpass('Enter your password: ')
return (username, password) hash_pwd = hashing(password)
return (username, hash_pwd)
def authenticate(username, password, pwdb): def authenticate(username, password, pwdb):
return password == pwdb[username] return password == pwdb[username]
def add_user(username, pwdb): def add_user(username, pwdb):
pwdb[username] = input(f'Enter password for {username}: ') password = input(f'Enter password for {username}: ')
pwdb[username] = hashing(password)
return pwdb return pwdb
def read_pwdb(PWDB_PATH): def read_pwdb(PWDB_PATH):