· 6 years ago · Nov 06, 2019, 01:22 PM
1## Licensed under GPLv3: https://www.gnu.org/licenses/gpl-3.0.html
2import hashlib
3import sqlite3
4
5def login(db):
6 def register(username):
7 pwd=hashlib.new('sha512') ## Asks for password, and adds it to the table
8 print("""
9 ------------------
10 REGISTER
11 ------------------""")
12 pwd.update(input("What do you want your password to be? ").encode()) ## Hash the password in sha512
13 password=pwd.hexdigest()
14 insert=(username,str(password))
15 c.execute('INSERT INTO logins VALUES (?,?)', insert)
16 print(username, "successfully registered")
17
18
19 failed=True
20 while (failed==True):
21 ## Connects to database
22 conn=sqlite3.connect(db)
23 c=conn.cursor()
24
25 ## Creates the login table in the database
26 c.execute ("""CREATE TABLE IF NOT EXISTS logins(
27 username text,
28 password text)""")
29
30
31
32 ## Asks user for their username
33 username = input("What's your username? ")
34 uname=(username,)
35
36 ## Searches the database for their username
37 c.execute('SELECT username FROM logins WHERE username=?',uname)
38
39
40 if (c.fetchone() != None): ## If there is a match in the database:
41 c.execute('SELECT password FROM logins WHERE username=?',uname) ## Search database for their password
42
43 ## Asks for and HTML escapes their password
44 pwd=hashlib.new('sha512')
45 pwd.update(input("What is the password you registered with? ").encode())
46 password=pwd.hexdigest()
47 ## Makes sure both input password and password found in database are strings
48 pw=str(("('"+str(password)+"',)"))
49 sqpw=str(c.fetchone())
50
51 ## Checks if they match
52 if(pw == sqpw):
53 print("login successful")
54 failed=False
55 return True
56 else:
57 print("Wrong password!!!!")
58 else: ## If their username is not found, redirect to registration
59 print("Username not found. Proceeding to register form...")
60 register(username)
61
62 ##
63 conn.commit()
64 conn.close()
65
66
67
68#########################################################################################################################
69 # MAIN PROGRAM #
70#########################################################################################################################
71
72def main_program(db,firstrun):
73 conn=sqlite3.connect(db)
74 c=conn.cursor()
75 c.execute("DROP TABLE IF EXISTS info")
76 c.execute("""CREATE TABLE IF NOT EXISTS info(
77 name text,
78 album text,
79 artist text,
80 year integer)""")
81
82
83 values=[("creep","pablo honey","radiohead",int(1993)),
84 ("money", "dark side of the moon", "pink floyd", int(1973)),
85 ("joe mama", "catch of the day", "fishwacker", int(2004)),
86 ("all star", "single", "smash mouth", int(1999)),
87 ("revenge", "single", "captainsparklez", int(2011)),
88 ("despacito","single","luis fonsi", int(2017))]
89 c.executemany("INSERT INTO info VALUES (?,?,?,?)",values)
90
91
92
93
94
95
96
97def checkFirstRun():
98 f=open("firstrun.txt","w+")
99 print(f.read())
100 if (str(f.read())!="false"):
101 firstrun=True
102 f.write("false")
103 print("This is your first run of the program, initialising table...")
104 return True
105 f.close()
106
107
108firstrun=checkFirstRun()
109
110#login_success=login("exam.db")
111#if (login_success==True):
112# main_program("exam.db")
113
114#print(login_success)