44
55SECRET_STORAGE_FILE = "secret.json"
66
7+
78def 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 )
911 return totp .now ()
1012
13+
1114def 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 )
1317 return totp .verify (code , valid_window = window )
1418
19+
1520def save_secret (secret_key , filename ):
1621 data = {'secret_key' : secret_key }
1722 with open (filename , 'w' ) as file :
1823 json .dump (data , file )
1924 return f'Secret key saved to { filename } '
2025
26+
2127def load_secret (filename ):
2228 try :
2329 with open (filename , 'r' ) as file :
@@ -27,16 +33,24 @@ def load_secret(filename):
2733 print (f'Secret key file "{ filename } " not found.' )
2834 return None
2935
36+
3037def 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' )
3342 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' )
3851 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' )
4054 args = parser .parse_args ()
4155
4256 if args .load :
@@ -53,17 +67,19 @@ def main():
5367 return
5468
5569 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 )
5772 print (f'Generated TOTP code: { code } ' )
5873
5974 if args .verify :
6075 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 )
6278 if result :
6379 print ('TOTP code is valid.' )
6480 else :
6581 print ('TOTP code is NOT valid.' )
6682
83+
6784if __name__ == '__main__' :
6885 main ()
69-
0 commit comments