Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from getpass import *
- import hashlib
- import os
- import sys # Imports :D
- import sqlite3
- import time
- def login():
- clear() # This is referring to the clear definition at the bottom
- print("Welcome to SirRobo's Login Screen ( Type -s to Signup )\n")
- username = input("Username: ") # Asks for the username
- if username == "-s": # If you said -s above, it will take you to the signup definition.
- signup() # ^
- password = getpass("Password: ") # This will ask for a password if -s wasn't entered above.
- c.execute("SELECT * FROM passwords WHERE username = ? AND password = ?",\
- (username, hasher(password))) # This is now selecting the part of the database where your username and password are
- if c.fetchone() is not None:
- print("\n [+] Logged In.\n\nWelcome {}\n".format(username)) # If your username and password are in the database, it will
- sys.exit() # log you in, if not it will say invalid.
- else:
- print("\n [!] Invalid Login.\n")
- time.sleep(1.5)
- login()
- def signup(): # THe signup definition :D
- clear()
- print("Welcome to SirRobo's Signup Screen ( Type -d to delete database )\n")
- username = input("New Username: ") # This will ask for username to store in the database.
- if username == "-d": # If you input -d however, it will run this code
- os.remove(Creds) # This will delete the database entirely.
- makeDB() # This will run the makedb def, which makes an emtpey database
- signup() # This will then re-run the signup definition.
- password = getpass("New Password: ") # Gets password to store in db.
- c.execute("INSERT INTO passwords VALUES('{}', '{}')".format(username, hasher(password))) # This will take your username
- con.commit() # This will finallize the changes ( Inserting Username and password ). # and password and store them in db
- login() # This will move on to the login def now that your info is in the DB so you can sign in.
- def clear(): # The clear def.
- os.system("clear") # If you are on windows, replace 'clear' to 'cls'
- def hasher(string):
- return hashlib.sha1(string.encode()).hexdigest() # This will hash your password using the sha1 hash, ( Can be changed ). This is
- def makeDB():# This def makes the database file. # Done to secure your password.
- global con # Some global variables
- global c
- con = sqlite3.connect(Creds) # This creates the database file.
- c = con.cursor() # This creates a cursor for selecting info.
- c.execute("CREATE TABLE IF NOT EXISTS passwords(username TEXT, password TEXT)") # This creates a table to store the username
- return c # and password
- if __name__ == '__main__':
- Creds = "Info.db" # Makes Creds = "info.db", then name of the database we are going to have
- makeDB() # Now that we have the creds var, go to the makedb def and start the program up :D
- login() # Now that we have our database made, go to the login definition.
- # You can use this for what ever you want, I just hope that you learnt something. :)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement