-
Notifications
You must be signed in to change notification settings - Fork 14
/
auth.py
74 lines (63 loc) · 2.01 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import getpass
import pathlib
import pickle
import random
import string
import tempfile
PWDB_FLNAME = pathlib.Path('pwdb.pkl')
CHARS = string.ascii_letters + string.digits + string.punctuation
def get_credentials():
username = input('Enter your username: ')
password = getpass.getpass('Enter your password: ')
return (username, password)
def authenticate(username, pass_text, pwdb):
if username in pwdb:
salt = pwdb[username][1]
if pwhash(pass_text, salt) == pwdb[username][0]:
return True
return False
def add_user(username, password, salt, pwdb, pwdb_file):
if username in pwdb:
raise Exception('Username already exists [%s]' %username)
else:
pwdb[username] = (pwhash(password,salt), salt)
write_pwdb(pwdb, pwdb_file)
def read_pwdb(pwdb_file):
try:
pwdb = pickle.load(pwdb_file)
pwdb_file.seek(0)
except EOFError:
pwdb = {}
return pwdb
def write_pwdb(pwdb, pwdb_file):
pickle.dump(pwdb, pwdb_file)
def pwhash(pass_text, salt):
hash_ = 0
full_pass_text = pass_text + salt
for idx, char in enumerate(full_pass_text):
# use idx as a multiplier, so that shuffling the characters returns a
# different hash
hash_ += (idx+1)*ord(char)
return hash_
def get_salt():
salt_chars = random.choices(CHARS, k=10)
return ''.join(salt_chars)
if __name__ == '__main__':
pwdb_path = tempfile.gettempdir() / PWDB_FLNAME
try:
pwdb_file = open(pwdb_path, 'rb+')
except FileNotFoundError:
pwdb_file = open(pwdb_path, 'wb+')
username, password = get_credentials()
pwdb = read_pwdb(pwdb_file)
if authenticate(username, password, pwdb):
print('Authentication succeeded!')
print(pwdb)
else:
print('Wrong username or password')
ans = input('Create new user [y/n]? ')
if ans == 'y':
salt = get_salt()
add_user(username, password, salt, pwdb, pwdb_file)
else:
print('Exit!')