diff --git a/auth.py b/auth.py index 1559a08..e0b0a90 100644 --- a/auth.py +++ b/auth.py @@ -1,6 +1,7 @@ import json import sys from getpass import getpass +import hashlib PWDB_PATH = 'pwdb.json' @@ -10,10 +11,10 @@ def get_credentials(): return (username, password) def authenticate(username, password, pwdb): - return password == pwdb[username] + return pwhash(password) == pwdb[username] def add_user(username, pwdb): - pwdb[username] = input(f'Enter password for {username}: ') + pwdb[username] = pwhash(input(f'Enter password for {username}: ')) return pwdb def read_pwdb(PWDB_PATH): @@ -24,6 +25,13 @@ def read_pwdb(PWDB_PATH): pwdb = {} return pwdb + +def pwhash(password): + hashed_pass = hashlib.sha256(password.encode('utf-8')).hexdigest() + return hashed_pass + + + def write_pwdb(pwdb, PWDB_PATH): pwdb_file = open(PWDB_PATH, 'wt') json.dump(pwdb, pwdb_file) @@ -39,10 +47,10 @@ if __name__ == "__main__": else: username, password = get_credentials() if username not in pwdb: - print('Wrong username!') + print('Wrong username or password!') else: if authenticate(username, password, pwdb): print('Successfully authenticated!') else: - print('Wrong password!') + print('Wrong username or password!')