-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (63 loc) · 2.16 KB
/
main.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
import hashlib # used for the pbkdf2 function
import os # used for generating random bytes = salt and working with files
users = {} # username, salt and hash value from the password are stored here
def register():
# add user
print("create username: ")
username = input()
print("create password: ")
password = input()
salt = os.urandom(128) # new salt for this user, size in bytes
key = hashlib.pbkdf2_hmac('sha512', password.encode('utf-8'), salt, 310000, 1024)
# (hash_name, password, salt, iterations, dklen) dklen is the length of the derived key. default length of hash alg
users[username] = { # store the salt and key
'salt': salt.hex(),
'key': key.hex()
}
# print(users.get(username))
# print(salt.hex())
# print(key.hex())
# print(users)
file1 = open("Password.secure", "a")
FileOutput = [username, str(users.get(username)), "°"]
file1.writelines(str(FileOutput))
file1.close()
print("registration successful")
# print("salt: " + str(salt))
# print("hash:" + str(key))
def login():
print("Enter username: ")
enteredUsername = input()
print("Enter password: ")
enteredPassword = input()
# print(users.values())
# print(users.get(1)) None
# print(users.get(username))
# print(users.keys()) root
if users.keys().__contains__(enteredUsername):
#print("user found")
enteredKey = hashlib.pbkdf2_hmac('sha512', enteredPassword.encode('utf-8'), users.values(), 310000, 1024).hex()
print("hashed password: " + enteredKey)
else:
print("user not found")
# create hash and compare
def main():
while True:
file = open("Password.secure")
my_list = []
for x in file:
my_list.append(x.split('°'))
# print(my_list)
# users
# print("users: " + users)
print("press (r) for register and (l) for log in: ")
choice = input()
if choice == "r":
register()
else:
if choice == "l":
login()
else:
print("invalid input. try again")
if __name__ == "__main__":
main()