-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalversion
87 lines (76 loc) · 2.86 KB
/
finalversion
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
import hashlib # used for the pbkdf2 function
import os # used for generating random bytes = salt and working with files
# import getpass # invisible passwort entry
users = {} # username, salt and hash value from the password are stored here
def register():
print("new username: ")
username = input()
print("new password: ")
password = input()
salt = os.urandom(128) # new salt for this user, size in bytes
try:
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()
}
except:
print("user could not be created")
try:
file1 = open("Password.secure", "a")
fileOutput = [username, users[username]]
file1.writelines(str(fileOutput))
file1.write("\n")
file1.close()
print("registration successful")
except:
print("the changes could not be saved")
def login():
print("enter username: ")
enteredUsername = input()
print("enter password: ")
enteredPassword = input()
try:
currentUser = users[enteredUsername]
enteredKey = hashlib.pbkdf2_hmac('sha512', enteredPassword.encode('utf-8'), bytes.fromhex(currentUser['salt']), 310000, 1024).hex()
if currentUser['key'] == enteredKey:
print("authentication succesful")
else:
print("wrong password")
except:
print("user not found")
def main():
while True:
print("press (r) for register, (l) for log in or (e) for exit: ")
choice = input()
if choice == "r":
register()
else:
if choice == "l":
login()
else:
if choice == "e":
print("see you around")
break
else:
print("invalid input. try again")
def loadDataFromFile():
try:
with open("Password.secure") as file:
for user in file:
user = user.rstrip()
saltIndex = str(user).find("salt")
keyIndex = str(user).find("key")
salt = str(user)[int(saltIndex + 8):int(keyIndex - 4)]
key = str(user)[int(keyIndex + 7):len(str(user)) - 3]
username = str(user)[2:int(saltIndex - 5)]
users[username] = {
'salt': salt,
'key': key
}
except:
print("Data could not be imported!")
if __name__ == "__main__":
loadDataFromFile() # executed at start to feed the dictionary with all user data
main()