-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers.py
55 lines (45 loc) · 1.34 KB
/
users.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
47
48
49
50
51
52
53
import json
import time
import hashlib
# fetch the Users file
def fetchUsers():
jsonFile = open('./JSON/users.json', 'r')
jsonData = json.load(jsonFile)
jsonFile.close()
return jsonData
# save the changes we did on the Users file
def saveUsers(jsonData):
f = open('./JSON/users.json', 'w')
newJson = json.dumps(jsonData)
f.write(newJson)
f.close()
def register(name,password):
if(name=="" or password==""):
print("name and password cannot remain empty")
return False
if(not userExist(name)):
hashed_password = hashlib.sha256(str(password).encode()).hexdigest()
user = {
"user":name,
"password":hashed_password
}
users=fetchUsers()
users.append(user)
saveUsers(users)
return True
# check if a user exist
def userExist(name):
users = fetchUsers()
for x in users:
if name == x["user"]:
return True
return False
# check if a user exist in the DB and log him in the session users
def validateLogin(name,password):
users = fetchUsers()
# hashing the password to compare with the database
hashed_password = hashlib.sha256(str(password).encode()).hexdigest()
for x in users:
if name == x["user"] and hashed_password == x["password"]:
return True
return False