From d03390dc8863097127cabd5ad9efa37d164b010d Mon Sep 17 00:00:00 2001 From: Laura Ana Maria Bostan Date: Mon, 20 Jan 2020 14:36:24 +0100 Subject: [PATCH 1/5] Add a 1st hash version . --- auth.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/auth.py b/auth.py index f50029c..d86292f 100644 --- a/auth.py +++ b/auth.py @@ -1,6 +1,7 @@ import getpass import pickle import sys +import numpy as np def get_credentials(): username = input('Enter your username: ') @@ -9,7 +10,7 @@ def get_credentials(): def authenticate(username, password, pwdb): if username in pwdb: - if password == pwdb[username]: + if pwhash(password) == pwdb[username]: return True return False @@ -23,9 +24,14 @@ def write_pwdb(pwdb, pwdb_file): pickle.dump(pwdb, pwdb_file) def add_user(username, password, pwdb): - pwdb[username] = password + pwdb[username] = pwhash(password) return pwdb +def pwhash(password): + password_encoded = sum(np.array([ord(c) for c in password])) + + + if __name__ == '__main__': DEFAULT_PWDB = 'pwdb.pkl' From 0eff83a9acfa4ef3d7f9778f10d74f87d11d8f3a Mon Sep 17 00:00:00 2001 From: Laura Ana Maria Bostan Date: Mon, 20 Jan 2020 15:04:18 +0100 Subject: [PATCH 2/5] Change te pwhash --- auth.py | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/auth.py b/auth.py index d86292f..2b3829e 100644 --- a/auth.py +++ b/auth.py @@ -1,59 +1,68 @@ import getpass import pickle import sys -import numpy as np +import os +import hashlib + def get_credentials(): - username = input('Enter your username: ') - password = getpass.getpass('Enter your password: ') + username = input("Enter your username: ") + password = getpass.getpass("Enter your password: ") return username, password + def authenticate(username, password, pwdb): if username in pwdb: if pwhash(password) == pwdb[username]: return True return False + def read_pwdb(pwdb_file): pwdb_file.seek(0) pwdb = pickle.load(pwdb_file) return pwdb + def write_pwdb(pwdb, pwdb_file): pwdb_file.seek(0) pickle.dump(pwdb, pwdb_file) + def add_user(username, password, pwdb): pwdb[username] = pwhash(password) return pwdb -def pwhash(password): - password_encoded = sum(np.array([ord(c) for c in password])) - +def pwhash(password, salt=None): + salt = "2e8c44d" + H = hashlib.sha3_512() + H.update(password.encode()) + return H.hexdigest() -if __name__ == '__main__': - DEFAULT_PWDB = 'pwdb.pkl' +if __name__ == "__main__": + DEFAULT_PWDB = "pwdb.pkl" try: - pwdb_file = open(DEFAULT_PWDB, 'rb+') + pwdb_file = open(DEFAULT_PWDB, "rb+") except FileNotFoundError: - pwdb_file = open(DEFAULT_PWDB, 'wb') + pwdb_file = open(DEFAULT_PWDB, "wb") pickle.dump({}, pwdb_file) pwdb_file.close() - print('Created empty pw database!') + print("Created empty pw database!") sys.exit(0) username, password = get_credentials() pwdb = read_pwdb(pwdb_file) if authenticate(username, password, pwdb): - print('Successfull authentication', username, password) + print("Successfull authentication", username, password) else: - ans = input('User not known or password is wrong. Do you want to add the ' - 'user to the password database? [y/n]') + ans = input( + "User not known or password is wrong. Do you want to add the " + "user to the password database? [y/n]" + ) - if ans == 'y': + if ans == "y": add_user(username, password, pwdb) write_pwdb(pwdb, pwdb_file) - From 67ae9f47cb14d27b465a8e45ea9230f2a3067df1 Mon Sep 17 00:00:00 2001 From: Laura Ana Maria Bostan Date: Mon, 20 Jan 2020 15:09:51 +0100 Subject: [PATCH 3/5] Remove salt --- auth.py | 1 - 1 file changed, 1 deletion(-) diff --git a/auth.py b/auth.py index 2b3829e..ea2c5a7 100644 --- a/auth.py +++ b/auth.py @@ -35,7 +35,6 @@ def add_user(username, password, pwdb): def pwhash(password, salt=None): - salt = "2e8c44d" H = hashlib.sha3_512() H.update(password.encode()) return H.hexdigest() From c05fe8f9f6476ff95647e322874264295151cb38 Mon Sep 17 00:00:00 2001 From: Laura Ana Maria Bostan Date: Mon, 20 Jan 2020 16:02:48 +0100 Subject: [PATCH 4/5] Salting --- auth.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/auth.py b/auth.py index 4ee3e32..5671171 100644 --- a/auth.py +++ b/auth.py @@ -14,7 +14,8 @@ def get_credentials(): def authenticate(username, password, pwdb): if username in pwdb: - if pwhash(password) == pwdb[username]: + salt = pwdb[username][1] + if pwhash(password, salt) == pwdb[username][0]: return True return False @@ -31,15 +32,16 @@ def write_pwdb(pwdb, pwdb_file): def add_user(username, password, pwdb): - pwdb[username] = pwhash(password) + salt = secrets.token_hex(32) + pwdb[username] = (pwhash(password, salt), salt) return pwdb -def pwhash(password, salt=None): - salt = secrets.token_hex(32) +def pwhash(password, salt): H = hashlib.sha3_512() H.update(password.encode()) - return H.hexdigest() + salt + H.update(salt.encode()) + return H.hexdigest() if __name__ == "__main__": From 78a8dc5a3d3e1520aa2f478074ec3a349825d809 Mon Sep 17 00:00:00 2001 From: Laura Ana Maria Bostan Date: Mon, 20 Jan 2020 16:09:28 +0100 Subject: [PATCH 5/5] Remove unused import --- auth.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/auth.py b/auth.py index 5671171..cf48534 100644 --- a/auth.py +++ b/auth.py @@ -1,7 +1,6 @@ import getpass import pickle import sys -import os import hashlib import secrets @@ -33,7 +32,7 @@ def write_pwdb(pwdb, pwdb_file): def add_user(username, password, pwdb): salt = secrets.token_hex(32) - pwdb[username] = (pwhash(password, salt), salt) + pwdb[username] = pwhash(password, salt), salt return pwdb