Skip to content

Commit 42fbfe7

Browse files
Merge pull request #2743 from andoriyaprashant/branch31
Multi-Factor Authentication Generator script added
2 parents e743c6a + 228dd71 commit 42fbfe7

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Multi-Factor Authentication (MFA) Generator Script
2+
3+
This is a Python script that provides a command-line interface for generating and verifying Time-Based One-Time Passwords (TOTPs) for Multi-Factor Authentication (MFA). The script uses the `pyotp` library for TOTP calculations.
4+
5+
## Features
6+
7+
- Generate TOTP codes using different hash algorithms, code lengths, and time intervals.
8+
- Verify TOTP codes and set a verification window for code validity.
9+
- Save and load secret keys from JSON files for easier management.
10+
11+
## Prerequisites
12+
13+
Before using the script, make sure you have Python installed. You can install the required `pyotp` library using the following command:
14+
15+
```bash
16+
pip install pyotp
17+
```
18+
19+
## Usage
20+
21+
1. Generate TOTP Code
22+
23+
To generate a TOTP code, run:
24+
25+
```bash
26+
python multi_factor_auth.py --generate --algorithm SHA1 --digits 6 --interval 30
27+
```
28+
29+
2. Verify TOTP Code
30+
31+
To verify a TOTP code, run:
32+
33+
```bash
34+
python multi_factor_auth.py --verify CODE_TO_VERIFY --algorithm SHA1 --digits 6 --interval 30 --window 1
35+
```
36+
37+
Replace CODE_TO_VERIFY with the TOTP code you want to verify.
38+
39+
3. Save and Load Secret Keys
40+
41+
To save a secret key to a JSON file, run:
42+
43+
```bash
44+
python multi_factor_auth.py --save secret.json
45+
```
46+
47+
To load a secret key from a JSON file, run:
48+
```bash
49+
python multi_factor_auth.py --load
50+
```
51+
52+
## Options
53+
54+
- --generate: Generate a TOTP code.
55+
- --verify: Verify a TOTP code.
56+
- --algorithm: Choose hash algorithm (SHA1, SHA256, SHA512).
57+
- --digits: Set number of digits in TOTP code (6 or 8).
58+
- --interval: Set time interval for TOTP code generation.
59+
- --window: Set verification window for TOTP codes.
60+
- --save: Save secret key to a JSON file.
61+
- --load: Load secret key from a JSON file.
62+
63+
## Example
64+
65+
Generate a TOTP code:
66+
67+
```bash
68+
python multi_factor_auth.py --generate --algorithm SHA256 --digits 8 --interval 60
69+
```
70+
71+
Verify a TOTP code:
72+
73+
```bash
74+
python multi_factor_auth.py --verify 123456 --algorithm SHA256 --digits 8 --interval 60 --window 2
75+
```
76+
77+
## Note
78+
79+
This script is provided for educational purposes. When using it for real-world applications, make sure to follow security best practices and ensure proper handling of secret keys.
80+
81+
## Contributing
82+
83+
If you have any ideas, improvements, or bug fixes, feel free to open an issue or submit a pull request. We appreciate your contributions!
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import pyotp
2+
import argparse
3+
import json
4+
5+
SECRET_STORAGE_FILE = "secret.json"
6+
7+
def generate_totp(secret_key, algorithm='SHA1', digits=6, interval=30):
8+
totp = pyotp.TOTP(secret_key, digits=digits, interval=interval, digest=algorithm)
9+
return totp.now()
10+
11+
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)
13+
return totp.verify(code, valid_window=window)
14+
15+
def save_secret(secret_key, filename):
16+
data = {'secret_key': secret_key}
17+
with open(filename, 'w') as file:
18+
json.dump(data, file)
19+
return f'Secret key saved to {filename}'
20+
21+
def load_secret(filename):
22+
try:
23+
with open(filename, 'r') as file:
24+
data = json.load(file)
25+
return data['secret_key']
26+
except FileNotFoundError:
27+
print(f'Secret key file "{filename}" not found.')
28+
return None
29+
30+
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')
33+
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')
38+
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')
40+
args = parser.parse_args()
41+
42+
if args.load:
43+
secret_key = load_secret(SECRET_STORAGE_FILE)
44+
if secret_key is None:
45+
return
46+
elif args.generate or args.verify:
47+
secret_key = input('Enter your secret key: ').strip()
48+
if args.save:
49+
save_result = save_secret(secret_key, args.save)
50+
print(save_result)
51+
else:
52+
print('Please specify either --generate or --verify.')
53+
return
54+
55+
if args.generate:
56+
code = generate_totp(secret_key, args.algorithm, args.digits, args.interval)
57+
print(f'Generated TOTP code: {code}')
58+
59+
if args.verify:
60+
code_to_verify = args.verify
61+
result = verify_totp(secret_key, code_to_verify, args.algorithm, args.digits, args.interval, args.window)
62+
if result:
63+
print('TOTP code is valid.')
64+
else:
65+
print('TOTP code is NOT valid.')
66+
67+
if __name__ == '__main__':
68+
main()
69+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pyotp
2+
argparse

0 commit comments

Comments
 (0)