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

USERNAME_CHARACTERS=string.digits+string.ascii_letters
PASSWORD_CHARACTERS=string.digits+string.ascii_letters+string.punctuation

SALT_CHARACTERS=&quot;0123456789abcdefghijklmnopqrstuv&quot;
SALT_CHARACTER_BITS=5
SALT_LENGTH=16
PEPPER_LENGTH=1

FILE_PATH=&quot;./shad&quot;
FILE_SEPERATOR=&quot;:&quot;

def int_to_string(num):
    string=&quot;&quot;
    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()

def get_user_data(username):
    with open(FILE_PATH) as h:
        data=h.read()
    for line in data.split(&quot;\n&quot;):
        l=line.split(FILE_SEPERATOR)
        if l[0]==username:
            return {&quot;user&quot;:l[0],
                    &quot;pass&quot;:l[1],
                    &quot;salt&quot;:l[2]}
    raise ValueError(&quot;Username not found: &quot;+username)


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

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

def add_user(username, password):
    if not(is_valid_username(username)):
        print(&quot;Invalid username&quot;)
        return False
    elif not(is_valid_password(password)):
        print(&quot;Invalid password&quot;)
        return False
    with open(FILE_PATH) as h:
        data=h.read()
    for line in data.split(&quot;\n&quot;):
        l=line.split(FILE_SEPERATOR)
        if l[0]==username:
            print(&quot;Username already exists&quot;)
            return False
        
    salt=generate_new_salt()
    data=FILE_SEPERATOR.join([username, do_hash(generate_new_pepper()+salt+password), salt])+&quot;\n&quot;
    with open(FILE_PATH, &quot;a&quot;) as h:
        h.write(data)
    return True

def login(username, password):
    if not(is_valid_username(username)):
        print(&quot;Invalid username&quot;)
        return False
    elif not(is_valid_password(password)):
        print(&quot;Invalid password&quot;)
        return False
    data=get_user_data(username)
    for pepper in generate_all_peppers():
        if do_hash(pepper+data[&quot;salt&quot;]+password)==data[&quot;pass&quot;]:
            return True
    return False
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__":)