Record#
- Today's study is about password security, salting and hashing passwords.
- The salt should be unique and not repeated, and each user has their own salt value.
- The purpose of salt is to prevent passwords from being reversed through collision, such as using a rainbow table by hackers.
- Hash is an algorithm, and there is also MD5.
- Today's exercise is to write a login program, which includes two functions: creating a user and user login.
CODE#
from replit import db
import random
print("🌟Login System🌟")
def AddUser():
username = input("Username > ")
password = input("Password > ")
salt = random.randint(1000, 9999)
newpass = hash(f"{password}{salt}")
db[username] = {"password": newpass, "salt": salt}
print("Success")
def Login():
username = input("Username > ")
password = input("Password > ")
salt = db[username]["salt"]
newpass = hash(f"{password}{salt}")
if db[username]:
if newpass == db[username]["password"]:
print(f"{db[username]}, Welcome!")
else:
print("Sorry1")
else:
print("Sorry2")
while True:
menu = input("1: Add User\n2: Login\n")
if menu == "1":
AddUser()
elif menu == "2":
Login()
Translation: