-
Notifications
You must be signed in to change notification settings - Fork 0
/
end.py
100 lines (75 loc) · 2.65 KB
/
end.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# page 83 in Python Magazine
import time
import hashlib
from getpass import getpass
LOGINS_FILENAME = "logins.txt"
class UserStore:
"""A simple user database.
>>> import os
>>> os.unlink('test.txt')
>>> u = UserStore('test.txt')
>>> u.check_exists('foo')
False
>>> u.add_user('foo', 'bar')
>>> u.check_exists('foo')
True
"""
def __init__(self, filename):
self._filename = filename
self._users = self._loadFromFile()
def _loadFromFile(self):
users = {}
try:
with open(self._filename, "r") as f:
for line in f:
parts = line.split()
if len(parts) == 2:
username, password = parts
users[username] = password
except FileNotFoundError:
pass
return users
def add_user(self, username, password):
hashed = hashPasswd(password)
self._users[username] = hashed
with open(self._filename, "a") as f:
f.write(username + " " + hashed + "\n")
def check_exists(self, username):
return username in self._users
def check_password(self, username, password):
return self._users.get(username) == hashPasswd(password)
def mainMenu():
users = UserStore(LOGINS_FILENAME)
while True:
status = input("Do you have a login account? y/n Or press q to quit.")
if status == "y":
oldUser(users)
elif status == "n":
newUser(users)
elif status == "q":
quit()
def newUser(users):
createLogin = input("Create a login name: ")
if users.check_exists(createLogin):
print("\nSorry, that login name already exists. Please try a different one.\n")
else:
createPassw = getpass("Create password: ")
users.add_user(createLogin, createPassw)
print("User created!")
def oldUser(users):
login = input("Enter the login name: ")
passw = getpass("Enter password: ")
# Check if user exists and login matches password
if users.check_password(login, passw):
print("\nLogin successful!\n")
print("User: ", login, "accessed the system on: ", time.asctime())
else:
print("\nUser doesn't exist or wrong password!\n")
def hashPasswd(plain):
"""Return the SHA256 hex digest of a string.
>>> hashPasswd('hello')
'2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
"""
return hashlib.sha256(plain.encode()).hexdigest()
if __name__ == "__main__":
mainMenu()