Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. from getpass import *
  2. import hashlib
  3. import os
  4. import sys # Imports :D
  5. import sqlite3
  6. import time
  7.  
  8. def login():
  9. clear() # This is referring to the clear definition at the bottom
  10.  
  11. print("Welcome to SirRobo's Login Screen ( Type -s to Signup )\n")
  12. username = input("Username: ") # Asks for the username
  13. if username == "-s": # If you said -s above, it will take you to the signup definition.
  14. signup() # ^
  15. password = getpass("Password: ") # This will ask for a password if -s wasn't entered above.
  16.  
  17. c.execute("SELECT * FROM passwords WHERE username = ? AND password = ?",\
  18. (username, hasher(password))) # This is now selecting the part of the database where your username and password are
  19. if c.fetchone() is not None:
  20. print("\n [+] Logged In.\n\nWelcome {}\n".format(username)) # If your username and password are in the database, it will
  21. sys.exit() # log you in, if not it will say invalid.
  22. else:
  23. print("\n [!] Invalid Login.\n")
  24. time.sleep(1.5)
  25. login()
  26.  
  27. def signup(): # THe signup definition :D
  28. clear()
  29.  
  30. print("Welcome to SirRobo's Signup Screen ( Type -d to delete database )\n")
  31.  
  32. username = input("New Username: ") # This will ask for username to store in the database.
  33. if username == "-d": # If you input -d however, it will run this code
  34. os.remove(Creds) # This will delete the database entirely.
  35. makeDB() # This will run the makedb def, which makes an emtpey database
  36. signup() # This will then re-run the signup definition.
  37. password = getpass("New Password: ") # Gets password to store in db.
  38.  
  39. c.execute("INSERT INTO passwords VALUES('{}', '{}')".format(username, hasher(password))) # This will take your username
  40. con.commit() # This will finallize the changes ( Inserting Username and password ). # and password and store them in db
  41.  
  42. login() # This will move on to the login def now that your info is in the DB so you can sign in.
  43.  
  44. def clear(): # The clear def.
  45. os.system("clear") # If you are on windows, replace 'clear' to 'cls'
  46. def hasher(string):
  47. return hashlib.sha1(string.encode()).hexdigest() # This will hash your password using the sha1 hash, ( Can be changed ). This is
  48. def makeDB():# This def makes the database file. # Done to secure your password.
  49. global con # Some global variables
  50. global c
  51. con = sqlite3.connect(Creds) # This creates the database file.
  52. c = con.cursor() # This creates a cursor for selecting info.
  53.  
  54. c.execute("CREATE TABLE IF NOT EXISTS passwords(username TEXT, password TEXT)") # This creates a table to store the username
  55. return c # and password
  56.  
  57. if __name__ == '__main__':
  58. Creds = "Info.db" # Makes Creds = "info.db", then name of the database we are going to have
  59. makeDB() # Now that we have the creds var, go to the makedb def and start the program up :D
  60. login() # Now that we have our database made, go to the login definition.
  61.  
  62. # 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