add hashing functionality

This commit is contained in:
ASPP Student 2025-09-22 16:21:54 +03:00
parent 33469b8840
commit 9c71173dc3

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 #base python lib to hash strings
def get_credentials(): def get_credentials():
username = input('Enter your username: ') username = input('Enter your username: ')
@ -8,11 +9,21 @@ def get_credentials():
return (username, password) return (username, password)
def authenticate(username, password, pwdb): 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): def add_user(username, pwdb):
pwdb[username] = getpass.getpass(f'Enter password for {username}: ') password = getpass.getpass(f'Enter password for {username}: ')
return pwdb password_hashed = hash_pass(password)
pwdb[username] = password_hashed
return pwdb
def read_pwdb(pwdb_path): def read_pwdb(pwdb_path):
try: try: