SHOW:
|
|
- or go back to the newest paste.
1 | - | import hashlib, getpass, os, inspect |
1 | + | import hashlib, getpass, os, inspect |
2 | ||
3 | - | def generate_hash(x, salt=b'$iS"A)u[Tq'): |
3 | + | def generate_hash(x, salt=b'$iS"A)u[Tq'): |
4 | - | crypt = hashlib.md5(); |
4 | + | crypt = hashlib.md5(); |
5 | - | crypt.update(salt + x.encode('utf-8')); |
5 | + | crypt.update(salt + x.encode('utf-8')); |
6 | ||
7 | - | return crypt.hexdigest(); |
7 | + | return crypt.hexdigest(); |
8 | ||
9 | - | def query_root_path(): |
9 | + | def query_root_path(): |
10 | - | return ''.join(os.path.split(os.path.join(inspect.getfile(inspect.currentframe())))[0]); |
10 | + | return ''.join(os.path.split(os.path.join(inspect.getfile(inspect.currentframe())))[0]); |
11 | ||
12 | - | def query_users_path(): |
12 | + | def query_users_path(): |
13 | - | return os.path.join(query_root_path(), 'users.txt'); |
13 | + | return os.path.join(query_root_path(), 'users.txt'); |
14 | ||
15 | - | def query_passwords_path(): |
15 | + | def query_passwords_path(): |
16 | - | return os.path.join(query_root_path(), 'password.txt'); |
16 | + | return os.path.join(query_root_path(), 'password.txt'); |
17 | ||
18 | - | def save(x, password=False): |
18 | + | def save(x, password=False): |
19 | - | with open(query_users_path() if not password else query_passwords_path(), mode="a+", encoding='utf-8') as file: |
19 | + | with open(query_users_path() if not password else query_passwords_path(), mode="a+", encoding='utf-8') as file: |
20 | - | file.write(x + "\n"); |
20 | + | file.write(x + "\n"); |
21 | ||
22 | - | def user_exists(x): |
22 | + | def user_exists(x): |
23 | - | with open(query_users_path()) as file: |
23 | + | with open(query_users_path()) as file: |
24 | - | test = file.read().split(); |
24 | + | test = file.read().split(); |
25 | - | return test.index(x) if x in test else None; |
25 | + | return test.index(x) if x in test else None; |
26 | ||
27 | - | def get_password(id): |
27 | + | def get_password(id): |
28 | - | with open(query_passwords_path()) as file: |
28 | + | with open(query_passwords_path()) as file: |
29 | - | text = file.read().split(); |
29 | + | text = file.read().split(); |
30 | - | return text[id] if len(text) >= id else None; |
30 | + | return text[id] if len(text) >= id else None; |
31 | ||
32 | - | def create_user(name=''): |
32 | + | def create_user(name=''): |
33 | - | user, password, confirm = input("Please input your username: ") if not name else name, getpass.getpass("Please input your password: "), getpass.getpass("Please confirm your password: "); |
33 | + | user, password, confirm = input("Please input your username: ") if not name else name, getpass.getpass("Please input your password: "), getpass.getpass("Please confirm your password: "); |
34 | ||
35 | - | while password != confirm: |
35 | + | while password != confirm: |
36 | - | print("\n"); |
36 | + | print("\n"); |
37 | - | password, confirm = getpass.getpass("Your passwords do not match. Please input your password: "), getpass.getpass("Please confirm your password: "); |
37 | + | password, confirm = getpass.getpass("Your passwords do not match. Please input your password: "), getpass.getpass("Please confirm your password: "); |
38 | ||
39 | - | save(user); |
39 | + | save(user); |
40 | - | save(generate_hash(password), True); |
40 | + | save(generate_hash(password), True); |
41 | ||
42 | - | if __name__ == '__main__': |
42 | + | if __name__ == '__main__': |
43 | - | if not os.path.exists(query_users_path()) or not os.path.exists(query_passwords_path()): |
43 | + | if not os.path.exists(query_users_path()) or not os.path.exists(query_passwords_path()): |
44 | - | print("Application cannot find user/password data. Creating new user and files."); |
44 | + | print("Application cannot find user/password data. Creating new user and files."); |
45 | - | create_user(); |
45 | + | create_user(); |
46 | else: | |
47 | - | user = input("User: "); |
47 | + | user = input("User: "); |
48 | - | id = user_exists(user); |
48 | + | id = user_exists(user); |
49 | ||
50 | if id == None: | |
51 | - | print("That user doesn't exist. Creating it..."); |
51 | + | print("That user doesn't exist. Creating it..."); |
52 | - | create_user(user); |
52 | + | create_user(user); |
53 | else: | |
54 | - | password , confirm = get_password(id), getpass.getpass(); |
54 | + | password , confirm = get_password(id), getpass.getpass(); |
55 | - | |
55 | + | |
56 | - | if id == None: |
56 | + | |
57 | - | print("An error prevented the system from retrieving your password."); |
57 | + | print("An error prevented the system from retrieving your password."); |
58 | - | else: |
58 | + | |
59 | - | while generate_hash(confirm) != password: |
59 | + | while generate_hash(confirm) != password: |
60 | - | confirm = getpass.getpass("Wrong password. Try again: "); |
60 | + | confirm = getpass.getpass("Wrong password. Try again: "); |
61 | - | |
61 | + | |
62 | - | print("Welcome back!"); |
62 | + | print("Welcome back!"); |