Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pwdb.pkl
33 changes: 26 additions & 7 deletions auth.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import pickle
from getpass import getpass
import random
import string

charset = string.ascii_letters + string.digits + string.punctuation

def get_credentials():
username = input("Enter username:")
password = input("Enter password:")
password = getpass("Enter password:")
return (username, password)

def authenticate(username, password, pwdb):
status = False
status = 0
if username in pwdb:
if password == pwdb[username]:
status = True
salt = pwdb[username]["salt"]
if pwhash(salt+password) == pwdb[username]["password"]:
status = 1
else:
print('Wrong password!')
pass
else:
add_user(username, password, pwdb)
status = 2

return status

def add_user(username, password, pwdb):
pwdb[username] = password
salt = get_salt()
pwdb[username] = {"password": pwhash(salt+password), "salt": salt}
write_pwdb(pwdb)

def read_pwdb():
Expand All @@ -34,12 +42,23 @@ def write_pwdb(pwdb):
with open("pwdb.pkl", "wb") as fh:
pickle.dump(pwdb, fh)

def pwhash(pwd):
sum = 0
for c in pwd:
sum += ord(c)
return sum

def get_salt():
return "".join(random.sample(charset,5))


if __name__ == "__main__":
username, password = get_credentials()
pwdb = read_pwdb()
status = authenticate(username, password, pwdb)
if status:
if status == 1:
print('Authentication succeeded:', pwdb)
elif status == 2:
print("New user created")
else:
print('Authentication failed')