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

adding salt to user passwords #12

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
18 changes: 12 additions & 6 deletions auth.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import random
import getpass
import pickle
import sys

def get_credentials():
username = input('Enter your username: ')
password = getpass.getpass('Enter your password: ')
return username, hash_string(password)
return username, password

def hash_string(inputstring):
def hash_string(inputstring, salt=None):
if salt is None:
salt = "".join(random.choice("1234567890qwertyuiopasdfghjklzxcvbnm")
for _ in range(16))
# hash the string
hashed_password = sum(ord(char) for char in inputstring)
return hashed_password
hashed_password = sum(ord(char) for char in salt+inputstring)
return salt, hashed_password

def authenticate(username, password, pwdb):
if username in pwdb:
if password == pwdb[username]:
salt, hashed_password = pwdb[username]
_, new_hashed_password = hash_string(password, salt)
if new_hashed_password == hashed_password:
return True
return False

Expand All @@ -28,7 +34,7 @@ def write_pwdb(pwdb, pwdb_file):
pickle.dump(pwdb, pwdb_file)

def add_user(username, password, pwdb):
pwdb[username] = password
pwdb[username] = hash_string(password)
return pwdb

if __name__ == '__main__':
Expand Down