-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcheck_user.py
executable file
·46 lines (36 loc) · 1.06 KB
/
check_user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 8 14:02:43 2019
@author: edoardottt
This file is under MIT License.
"""
import os
import sqlite3
import usage
import hashlib
db_filename = "database.db"
db_is_new = not os.path.exists(db_filename)
def create_user(conn, data, username):
sql = """ INSERT INTO users VALUES(?,?) """
cur = conn.cursor()
cur.execute(sql, data)
conn.commit()
# check if user in input exists in database.db
def check_if_user_exists(username, password):
if db_is_new:
usage.print_usage(5)
else:
conn = sqlite3.connect(db_filename)
cursor = conn.cursor()
p = hashlib.sha256(password.encode("utf-8")).hexdigest()
cursor.execute(
"SELECT * FROM users WHERE username = ? and password= ?", (username, p)
)
data = cursor.fetchone()
if data is None:
cred = (username, p)
create_user(conn, cred, username)
return False
else:
return True