-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_manager.py
69 lines (59 loc) · 2.41 KB
/
password_manager.py
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
import json
import os
from pathlib import Path
class PasswordManager:
def __init__(self):
self.passwords_file = "user_progress.json"
self.known_passwords = self._load_passwords()
# Known password for level 0
self.known_passwords.setdefault('bandit0', 'bandit0')
self._save_passwords()
def _load_passwords(self):
"""Load saved passwords from file"""
try:
if os.path.exists(self.passwords_file):
with open(self.passwords_file, 'r') as f:
return json.load(f)
except Exception as e:
print(f"Error loading passwords: {e}")
return {}
def _save_passwords(self):
"""Save passwords to file"""
try:
with open(self.passwords_file, 'w') as f:
json.dump(self.known_passwords, f, indent=4)
except Exception as e:
print(f"Error saving passwords: {e}")
def check_output_for_password(self, level: int, output: str) -> tuple[bool, str]:
"""
Check if the output contains a valid password for the next level.
Returns (is_password_found, password if found else '')
"""
# Clean the output
output = output.strip()
# If output is exactly 32 characters long, it might be a password
if len(output) == 32 and all(c in '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' for c in output):
next_level = f'bandit{level + 1}'
self.known_passwords[next_level] = output
self._save_passwords()
return True, output
return False, ''
def get_password(self, level: int) -> str:
"""Get password for a specific level"""
return self.known_passwords.get(f'bandit{level}', '')
def has_completed_level(self, level: int) -> bool:
"""Check if user has completed a specific level"""
return f'bandit{level + 1}' in self.known_passwords
def get_progress(self) -> dict:
"""Get user's progress (completed levels)"""
return {
'completed_levels': [
int(k.replace('bandit', '')) - 1
for k in self.known_passwords.keys()
if k != 'bandit0'
],
'highest_level': max([
int(k.replace('bandit', ''))
for k in self.known_passwords.keys()
] + [0])
}