4
4
5
5
SECRET_STORAGE_FILE = "secret.json"
6
6
7
+
7
8
def generate_totp (secret_key , algorithm = 'SHA1' , digits = 6 , interval = 30 ):
8
- totp = pyotp .TOTP (secret_key , digits = digits , interval = interval , digest = algorithm )
9
+ totp = pyotp .TOTP (secret_key , digits = digits ,
10
+ interval = interval , digest = algorithm )
9
11
return totp .now ()
10
12
13
+
11
14
def verify_totp (secret_key , code , algorithm = 'SHA1' , digits = 6 , interval = 30 , window = 1 ):
12
- totp = pyotp .TOTP (secret_key , digits = digits , interval = interval , digest = algorithm )
15
+ totp = pyotp .TOTP (secret_key , digits = digits ,
16
+ interval = interval , digest = algorithm )
13
17
return totp .verify (code , valid_window = window )
14
18
19
+
15
20
def save_secret (secret_key , filename ):
16
21
data = {'secret_key' : secret_key }
17
22
with open (filename , 'w' ) as file :
18
23
json .dump (data , file )
19
24
return f'Secret key saved to { filename } '
20
25
26
+
21
27
def load_secret (filename ):
22
28
try :
23
29
with open (filename , 'r' ) as file :
@@ -27,16 +33,24 @@ def load_secret(filename):
27
33
print (f'Secret key file "{ filename } " not found.' )
28
34
return None
29
35
36
+
30
37
def main ():
31
- parser = argparse .ArgumentParser (description = 'Multi-Factor Authentication (MFA) Generator' )
32
- parser .add_argument ('--generate' , action = 'store_true' , help = 'Generate a TOTP code' )
38
+ parser = argparse .ArgumentParser (
39
+ description = 'Multi-Factor Authentication (MFA) Generator' )
40
+ parser .add_argument ('--generate' , action = 'store_true' ,
41
+ help = 'Generate a TOTP code' )
33
42
parser .add_argument ('--verify' , help = 'Verify a TOTP code' )
34
- parser .add_argument ('--algorithm' , choices = ['SHA1' , 'SHA256' , 'SHA512' ], default = 'SHA1' , help = 'Hash algorithm for TOTP' )
35
- parser .add_argument ('--digits' , type = int , choices = [6 , 8 ], default = 6 , help = 'Number of digits in TOTP code' )
36
- parser .add_argument ('--interval' , type = int , default = 30 , help = 'Time interval for TOTP code generation' )
37
- parser .add_argument ('--window' , type = int , default = 1 , help = 'Verification window for TOTP codes' )
43
+ parser .add_argument (
44
+ '--algorithm' , choices = ['SHA1' , 'SHA256' , 'SHA512' ], default = 'SHA1' , help = 'Hash algorithm for TOTP' )
45
+ parser .add_argument (
46
+ '--digits' , type = int , choices = [6 , 8 ], default = 6 , help = 'Number of digits in TOTP code' )
47
+ parser .add_argument ('--interval' , type = int , default = 30 ,
48
+ help = 'Time interval for TOTP code generation' )
49
+ parser .add_argument ('--window' , type = int , default = 1 ,
50
+ help = 'Verification window for TOTP codes' )
38
51
parser .add_argument ('--save' , help = 'Save secret key to a file' )
39
- parser .add_argument ('--load' , action = 'store_true' , help = 'Load secret key from a file' )
52
+ parser .add_argument ('--load' , action = 'store_true' ,
53
+ help = 'Load secret key from a file' )
40
54
args = parser .parse_args ()
41
55
42
56
if args .load :
@@ -53,17 +67,19 @@ def main():
53
67
return
54
68
55
69
if args .generate :
56
- code = generate_totp (secret_key , args .algorithm , args .digits , args .interval )
70
+ code = generate_totp (secret_key , args .algorithm ,
71
+ args .digits , args .interval )
57
72
print (f'Generated TOTP code: { code } ' )
58
73
59
74
if args .verify :
60
75
code_to_verify = args .verify
61
- result = verify_totp (secret_key , code_to_verify , args .algorithm , args .digits , args .interval , args .window )
76
+ result = verify_totp (secret_key , code_to_verify ,
77
+ args .algorithm , args .digits , args .interval , args .window )
62
78
if result :
63
79
print ('TOTP code is valid.' )
64
80
else :
65
81
print ('TOTP code is NOT valid.' )
66
82
83
+
67
84
if __name__ == '__main__' :
68
85
main ()
69
-
0 commit comments