1
+ import hash
2
+
3
+ # TODO: Encapsulate this later
4
+ hasher = None
5
+
1
6
def configRead ():
2
7
import configparser
3
8
config = configparser .ConfigParser ()
@@ -8,11 +13,16 @@ def configRead():
8
13
except KeyError :
9
14
quiet = False
10
15
try :
11
- alg = config ['Config' ]['alg' ]
12
- alg = str (quiet ).lower ()
16
+ alg = config ['Config' ]['alg' ].lower ()
17
+ # alg = str(quiet).lower() Not really sure the purpose of this line
18
+ # Not the most elegant but trying to hit the KeyError on unsupported hash algs
19
+ if not (alg in hash .SUPPORTED_HASHES ):
20
+ raise KeyError
13
21
except KeyError :
14
22
print ("ERROR: Algorithm not defined. Reverting to SHA256" )
15
23
alg = 'sha256'
24
+ global hasher
25
+ hasher = hash .Hasher (alg )
16
26
try :
17
27
saltSize = config ['Salt' ]['saltSize' ]
18
28
saltSize = int (saltSize )
@@ -29,7 +39,6 @@ def salter(pswd,saltsize):
29
39
return pswd + salt ,salt
30
40
31
41
def init_usrs (nUsr , saltSize ):
32
- import hashlib
33
42
usrlist = []
34
43
for index in range (nUsr ):
35
44
usrname = ''
@@ -39,7 +48,10 @@ def init_usrs(nUsr, saltSize):
39
48
while pswd == '' :
40
49
pswd = input ("Enter password: " )
41
50
pswd ,salt = salter (pswd ,saltSize )
42
- pswd = hashlib .sha256 (pswd .encode ()).hexdigest ()
51
+ hasher .update (pswd .encode ())
52
+ pswd = hasher .hexdigest ()
53
+ # We have to clear the hasher every time with the new implementation, later we could define this behaviour automatically
54
+ hasher .clear_hasher ()
43
55
usrlist .append ([])
44
56
usrlist [index ].append (usrname )
45
57
usrlist [index ].append (pswd )
@@ -78,15 +90,35 @@ def save_users(usrlist,overWrite):
78
90
saveFile .write ('' )
79
91
saveFile .close ()
80
92
81
- def usr_check (usrn ,pswd ,usrlist ):
82
- import hashlib
93
+ def usr_check (usrn ,pswd ,usrlist , test_hasher : hash .Hasher = None ):
94
+ if test_hasher :
95
+ hasher = test_hasher
96
+ # TODO:
97
+ # Supporting different hashes means we have to figure out what hash was used for the usrlist
98
+ # This basic solution simply reads what hash was specified in the config file.
99
+ # The config file will need additional information, and this logic will have to be changed, if variable
100
+ # output for shake is allowed.
101
+ else :
102
+ import configparser
103
+ config = configparser .ConfigParser ()
104
+ config .read ('config.ini' )
105
+ try :
106
+ alg = config ['Config' ]['alg' ].lower ()
107
+ if not (alg in hash .SUPPORTED_HASHES ):
108
+ raise KeyError
109
+ except KeyError :
110
+ print ("No algorithm specified to read usrlist or unsuporrted algorithm\n Defaulting to sha256" )
111
+ alg = 'sha256'
112
+ hasher = hash .Hasher (alg )
83
113
index = 0
84
114
Ufound = False
85
115
Pfound = False
86
116
while Ufound == False and index < len (usrlist ):
87
117
if usrlist [index ][0 ]== usrn :
88
118
Ufound = True
89
- pswd = hashlib .sha256 (str (pswd + str (usrlist [index ][2 ])).encode ()).hexdigest ()
119
+ hasher .update (str (pswd + str (usrlist [index ][2 ])).encode ())
120
+ pswd = hasher .hexdigest ()
121
+ hasher .clear_hasher ()
90
122
if usrlist [index ][1 ]== pswd :
91
123
Pfound = True
92
124
break
@@ -118,11 +150,14 @@ def login_init(usrname,pswd,overWrite,quiet,saltSize):
118
150
print ("Userfile does not exist! Execute with overWrite set to 'True'" )
119
151
sys .exit ()
120
152
elif overWrite == True :
121
- if input ( "Do you want to overwrite the existing user list[y/N]? " ). upper () == 'Y' :
122
- newFile = True
123
- else :
124
- newFile = False
153
+ # Bug fix for when no usrlist exists but the user asks not to overwrite user list
154
+ newFile = True
155
+ if os . path . isfile ( "usrlist" ) and ( input ( "Do you want to overwrite the existing user list [Y/N]? " ). upper () == 'N' ) :
156
+ newFile = False
125
157
while True :
158
+ # TODO:
159
+ # Seems to me redundant to ask if they want to clear the database when "overwriting" the existing database does
160
+ # the same thing.
126
161
nUsr = input ("Enter the number of users to initialize ('0' Clears the database): " )
127
162
try :
128
163
nUsr = int (nUsr )
0 commit comments