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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pwdb.pkl
__pycache__/
32 changes: 23 additions & 9 deletions auth.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
import pickle
import os

from getpass import getpass

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

def pwhash(password, salt):
hashsum = 0
for char in password:
hashsum += ord(char)
return hashsum

def create_salt():
return os.urandom(5).hex()

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

status = 2
return status

def add_user(username, password, pwdb):
pwdb[username] = password
salt = create_salt()
pwdb[username] = [pwhash(password, salt), salt]
write_pwdb(pwdb)

def read_pwdb():
Expand All @@ -39,7 +51,9 @@ def write_pwdb(pwdb):
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("Registered new user")
else:
print('Authentication failed')