Home > OS >  Why isn't my function save_users(): working? How do I save these passwords to file?
Why isn't my function save_users(): working? How do I save these passwords to file?

Time:01-11

from hashlib import sha256
import random, sys


def hash(string):
    '''Hashes a string'''

    return sha256(
        string.encode()).hexdigest() # when using hash() returns a hashed string


def save_users():
    f = open("UserDetails.txt", "w")
    f.write("Username;{} \nPassword:{}".format(username, password))
    f.close


print("\nWelcome")
signIn = input("Do you have an account? [Y/N]").upper()

if signIn == "N":
    print("Sign up :")

    username = input("New Username: ")
    password = hash(input("New Password: "))
    confirm = hash(input("ConfirmPassword: ")) == password
    print(password)

    if not confirm:
        print("Passwords do not match")

save_users()

"print (password)" is just there for me to test if it actually hashed the string as this is my first time doing anything like this. How would I save the password and username to an email, and similarly how would I authenticate the password and username?

Python 3.8.5

CodePudding user response:

Replace:

def save_users():
    f = open("UserDetails.txt", "w")
    f.write("Username;{} \nPassword:{}".format(username, password))
    f.close

With:

def save_users(username, password):
    f = open("UserDetails.txt", "w")
    f.write("Username;{} \nPassword:{}".format(username, password))
    f.close()

And call it with save_users(username, password) instead of save_users()

CodePudding user response:

from hashlib import sha256
def hash(string):
    '''Hashes a string'''

    return sha256(
        string.encode()).hexdigest() 


def save_users(username, password):
    with open("UserDetails.txt", "w") as f:
        f.write("Username;{} \nPassword:{}".format(username, password))


print("\nWelcome")
signIn = input("Do you have an account? [Y/N]").upper()

if(signIn == "N"):
    print("Sign up :")
    username = input("New Username: ")
    password = hash(input("New Password: "))
    confirm = hash(input("ConfirmPassword: ")) == password

    if(confirm):
         save_users(username, password)
    else:
        print("Passwords do not match")

else:
    print("you are already have an account")

Test this

  •  Tags:  
  • Related