Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Password hashed and salted #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
43 changes: 33 additions & 10 deletions auth.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import pickle
from getpass import getpass
import random as rnd
import string

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

def get_hash(password, salt):
hash_sum = 0
for character in password:
hash_sum += ord(character)
for character in salt:
hash_sum += ord(character)
return hash_sum

def get_salt():
salt = []
for i in range (10):
character = rnd.choice(string.ascii_lowercase)
salt.append(character)
return ''.join(salt)

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

return status

def add_user(username, password, pwdb):
pwdb[username] = password
new_salt = get_salt()
pwdb[username] = (new_salt, get_hash(new_salt, password))
write_pwdb(pwdb)

def read_pwdb():
Expand All @@ -39,7 +59,10 @@ def write_pwdb(pwdb):
username, password = get_credentials()
pwdb = read_pwdb()
status = authenticate(username, password, pwdb)
if status:
if status == 0:
print('Username taken, authentication failed.')
if status == 1:
print('Authentication succeeded:', pwdb)
else:
print('Authentication failed')
if status == 2:
print('Username not in database, added.')