# Simple authentication and registration of users

I have created a simple authentication script in python that can add users and provides a login function. I've tried to thow as much security into this as possible, which probably wasn't the best idea.

To my knowledge, the code works fine and withstands as much invalid input as I can find (although I haven't written any proper tests). I can see straight away that I've repeated myself quite a lot, but I'm not entirely sure how to cut it down.

Usage:

login(username, password)
Checks if this username/password combination is valid.
Returns bool

add_user(username, password)
Adds a user to the password file.
Returns bool

import hashlib, random, string

SALT_CHARACTERS="0123456789abcdefghijklmnopqrstuv"
SALT_CHARACTER_BITS=5
SALT_LENGTH=16
PEPPER_LENGTH=1

FILE_SEPERATOR=":"

def int_to_string(num):
string=""
while num!=0:
c=num%(2**SALT_CHARACTER_BITS)
string+=SALT_CHARACTERS[c]
num-=c
num//=2**SALT_CHARACTER_BITS
return string

def generate_new_salt():
return int_to_string(random.getrandbits(SALT_CHARACTER_BITS*SALT_LENGTH))
def generate_new_pepper():
return int_to_string(random.getrandbits(SALT_CHARACTER_BITS*PEPPER_LENGTH))
def generate_all_peppers():
for i in range(1,SALT_CHARACTER_BITS*PEPPER_LENGTH):
yield int_to_string(i)

def do_hash(string):
return hashlib.sha3_256(string.encode()).hexdigest()

with open(FILE_PATH) as h:
for line in data.split("\n"):
l=line.split(FILE_SEPERATOR)
return {"user":l[0],
"pass":l[1],
"salt":l[2]}

for i in username:
if not(i in USERNAME_CHARACTERS):
return False
return True

for i in username:
if not(i in PASSWORD_CHARACTERS):
return False
return True

return False
return False
with open(FILE_PATH) as h:
for line in data.split("\n"):
l=line.split(FILE_SEPERATOR)
return False

salt=generate_new_salt()
with open(FILE_PATH, "a") as h:
h.write(data)
return True

return False
return False
for pepper in generate_all_peppers():
return True
return False

• You are using an uniterated hash that can be a problem as it is too fast. Although the characters allowed in passwords are very limited (allow all Unicode points, no need to restrict it). You should also make sure that the login function uses a constant time method to compare the password authenticator string. – eckes Sep 24 '17 at 21:58
• Homebrew, Hashing and security are never a good solution. There is a reason to use brypt, don't be a dave – Ludisposed Sep 25 '17 at 7:20
• @eckes Can you put answers in answer boxes, thanks. – Peilonrayz Sep 25 '17 at 8:19

Cryptography

As others have mentioned, I would use bcrypt

Format

You have used good function names and variable names. However, you can add doc strings for each function.

def add_user(username, password):
""""Function to add a user. Returns True for successful, or False for failure"""
#Function code here


Additionally, you could improve your compliance with PEP8 (the Python Style Guide) by adding white-space around your equals symbols. This would greatly improve the readability of your code. Also, a more relaxed vertical spacing would improve the look of your code.

__main__

Not to sure if you want to create an default function for your code. But you could add (if __name__ == "__main__":)

# Code style

You mostly stick to PEP8 regarding variable and function naming.
Though you might want to implement a bit more of vertical spacing between the functions.

# Homebrew cryprography

As already metioned by other users in the comments, homebrew crypto is - most of the time - a bad idea.
Instead you should rely on proven algorithms and libraries such as the metioned bcrypt or Argon2, which was elected winner in the Password Hashing Competition 2015.