forked from ASPP/2021-bordeaux-git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
58 lines (45 loc) · 1.24 KB
/
auth.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
import os
import json
PWDB_FILENAME = 'pwdb.json'
def get_credentials():
username = input('Type the username: ')
password = input('Type the password: ')
hashed_password = pwhash(password)
return username, hashed_password
def add_user(user, password, pwdb):
pwdb[user] = password
write_pwdb(pwdb)
return
def authenticate(user, password, pwdb):
if user in pwdb:
if password == pwdb[user]:
print('Successfully authenticated!')
else:
print('Wrong password!!')
else:
answer = input('Add user to the db? ')
if answer == 'y':
add_user(user, password, pwdb)
return
def pwhash(password):
hash_ = 0
for idx, char in enumerate(password):
hash_ += (idx+1)*ord(char)
return hash_
def write_pwdb(pwdb):
with open(PWDB_FILENAME, 'w') as fh:
json.dump(pwdb, fh)
def read_pwdb():
if os.path.exists(PWDB_FILENAME):
with open(PWDB_FILENAME, 'r') as fh:
pwdb = json.load(fh)
else:
pwdb = {}
write_pwdb(pwdb)
return pwdb
if __name__ == "__main__":
pwdb = read_pwdb()
user, password = get_credentials()
authenticate(user, password, pwdb)
write_pwdb(pwdb)
print(pwdb)