forked from EdvardPedersen/UsersAndStorage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.py
More file actions
70 lines (61 loc) · 1.94 KB
/
Copy pathusers.py
File metadata and controls
70 lines (61 loc) · 1.94 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pickle
import hashlib
class User:
def __init__(self, username, pwhash):
self.name = username
self.pwhash = pwhash
self.scores = []
def create_hash(username, password):
hasher = hashlib.sha256()
hasher.update(username.encode('utf-8'))
hasher.update(password.encode('utf-8'))
return hasher.digest()
def get_username_password():
username = input("Enter username: ")
password = input("Enter password: ")
hashed_pw = create_hash(username, password)
return username, hashed_pw
def create_user():
username, hashed_pw = get_username_password()
return User(username, hashed_pw)
def login_user(users):
username, hashed_pw = get_username_password()
if username not in users:
print("User does not exist")
elif hashed_pw != users[username].pwhash:
print("Wrong password!")
return users[username]
def get_score(user):
score = int(input("Provide score"))
user.scores.append(score)
if __name__ == "__main__":
try:
f = open("userdb", 'rb')
users = pickle.load(f)
except Exception as e:
users = {}
done = False
user = None
while not done:
command = input("Input command (h for help): ")
if command == 'c':
user = create_user()
users[user.name] = user
elif command == 'l':
user = login_user(users)
elif command == 'v':
print(user.name + " - " + str(user.scores))
elif command == 's':
get_score(user)
elif command == '':
done = True
elif command == 'h':
print("c - create user")
print("l - login to existing user")
print("v - view scores for current user")
print("s - input score for current user")
print("h - show this help")
print("blank line to quit")
with open("userdb", 'wb') as f:
pickle.dump(users, f)
f.close()