-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpassword_menager.py
More file actions
1330 lines (1064 loc) · 41 KB
/
Copy pathpassword_menager.py
File metadata and controls
1330 lines (1064 loc) · 41 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Secure Password Manager
A professional-grade local password manager with AES-256 encryption.
Features:
- Military-grade encryption (AES-256 via Fernet)
- Password generator with customization
- Password strength analysis
- Category organization
- Backup and restore functionality
- Completely local storage (no cloud)
Author: Built with security and ease-of-use in mind
"""
# flake8: noqa
import hmac
import json
import base64
import getpass
import secrets
import string
from datetime import datetime
from pathlib import Path
# Cryptography imports for secure encryption
from cryptography.fernet import Fernet, InvalidToken
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
# For beautiful terminal interface
try:
from colorama import init, Fore, Style
init(autoreset=True)
except ImportError:
# Fallback for when colorama is not installed
class _FallbackFore:
RED = GREEN = YELLOW = CYAN = BLUE = MAGENTA = WHITE = RESET = ""
class _FallbackStyle:
BRIGHT = RESET_ALL = ""
# Match the public API of colorama so rest of the script can use it
Fore = _FallbackFore()
Style = _FallbackStyle()
try:
from tabulate import tabulate
TABULATE_ENABLED = True
except ImportError:
TABULATE_ENABLED = False
# ============================================================================
# CONFIGURATION & CONSTANTS
# ============================================================================
# File paths for storing encrypted data
DATA_DIR = Path.home() / ".password_manager"
PASSWORDS_FILE = DATA_DIR / "passwords.enc"
SALT_FILE = DATA_DIR / "salt.key"
BACKUP_DIR = DATA_DIR / "backups"
# Security settings - these make your passwords very hard to crack
PBKDF2_ITERATIONS = 480000 # Number of times we hash the master password
# Higher = more secure but slightly slower
# 480,000 is OWASP recommended minimum for 2024
# Available password categories for organization
CATEGORIES = [
"Email",
"Banking",
"Social Media",
"Shopping",
"Work",
"Gaming",
"Entertainment",
"Other"
]
# ============================================================================
# ENCRYPTION & SECURITY FUNCTIONS
# ============================================================================
def derive_key_from_password(password: str, salt: bytes) -> bytes:
"""
Convert a master password into an encryption key using PBKDF2.
This is critical for security! We don't use the password directly.
Instead, we use PBKDF2 (Password-Based Key Derivation Function 2) which:
1. Adds a random salt (prevents rainbow table attacks)
2. Runs 480,000+ iterations (prevents brute force attacks)
3. Produces a strong 256-bit encryption key
Args:
password: Your master password (as string)
salt: Random bytes unique to your installation
Returns:
A 32-byte encryption key suitable for AES-256
"""
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(), # Use SHA-256 hashing algorithm
length=32, # Generate 32 bytes = 256 bits for AES-256
salt=salt, # Unique salt prevents pre-computed attacks
iterations=PBKDF2_ITERATIONS # High iteration count slows down attackers
)
key = kdf.derive(password.encode())
return base64.urlsafe_b64encode(key)
def initialize_security():
"""
Set up the security infrastructure on first run.
Creates:
- Main data directory (~/.password_manager/)
- Backup directory for encrypted backups
- Unique salt file (critical for security!)
The salt is randomly generated once and stored. It ensures that even
if two people use the same master password, their encryption keys
will be completely different.
"""
DATA_DIR.mkdir(exist_ok=True)
BACKUP_DIR.mkdir(exist_ok=True)
if not SALT_FILE.exists():
# Generate a cryptographically secure random salt
# This is created ONCE and never changes
salt = secrets.token_bytes(32) # 32 bytes = 256 bits of randomness
SALT_FILE.write_bytes(salt)
print(f"{Fore.GREEN}✓ Security initialized with unique encryption salt")
def get_salt() -> bytes:
"""
Load the unique salt used for key derivation.
Returns:
32 bytes of salt data
"""
if not SALT_FILE.exists():
raise FileNotFoundError("Salt file not found. Run initialization first.")
return SALT_FILE.read_bytes()
def encrypt_data(data: dict, key: bytes) -> bytes:
"""
Encrypt password data using Fernet (AES-256 in CBC mode).
Fernet provides:
- AES-256 encryption (industry standard)
- Authentication (detects tampering)
- Timestamp (for key rotation)
Args:
data: Dictionary containing all passwords
key: Encryption key derived from master password
Returns:
Encrypted bytes that can be safely stored on disk
"""
fernet = Fernet(key)
json_data = json.dumps(data, indent=2)
encrypted = fernet.encrypt(json_data.encode())
return encrypted
def decrypt_data(encrypted_data: bytes, key: bytes) -> dict:
"""
Decrypt password data and verify integrity.
Args:
encrypted_data: Encrypted bytes from file
key: Encryption key from master password
Returns:
Dictionary containing all passwords
Raises:
InvalidToken: If password is wrong or data is corrupted/tampered
"""
fernet = Fernet(key)
decrypted = fernet.decrypt(encrypted_data)
return json.loads(decrypted.decode())
# ============================================================================
# PASSWORD STRENGTH CHECKER
# ============================================================================
def check_password_strength(password: str) -> dict:
"""
Analyze a password and rate its strength.
Checks for:
- Length (longer is better)
- Character variety (uppercase, lowercase, numbers, symbols)
- Common patterns (repeated characters, sequences)
Args:
password: The password to analyze
Returns:
Dictionary with:
- score: 0-100 strength score
- level: "Weak", "Medium", "Strong", or "Very Strong"
- feedback: List of suggestions to improve the password
"""
score = 0
feedback = []
# Length is the most important factor
length = len(password)
if length >= 16:
score += 40
elif length >= 12:
score += 30
elif length >= 8:
score += 20
else:
score += 10
feedback.append("Use at least 12 characters (16+ is better)")
# Check for character variety
has_lower = any(c.islower() for c in password)
has_upper = any(c.isupper() for c in password)
has_digit = any(c.isdigit() for c in password)
has_symbol = any(c in string.punctuation for c in password)
variety_score = sum([has_lower, has_upper, has_digit, has_symbol])
score += variety_score * 10
if not has_lower:
feedback.append("Add lowercase letters")
if not has_upper:
feedback.append("Add uppercase letters")
if not has_digit:
feedback.append("Add numbers")
if not has_symbol:
feedback.append("Add symbols (!@#$%^&* etc.)")
# Check for repeated characters (like "aaa" or "111")
if len(set(password)) < len(password) * 0.5:
score -= 10
feedback.append("Avoid too many repeated characters")
# Check for common sequences
sequences = ["123", "abc", "qwerty", "password", "admin"]
if any(seq in password.lower() for seq in sequences):
score -= 15
feedback.append("Avoid common sequences and words")
# Bonus points for good length with variety
if length >= 16 and variety_score == 4:
score += 20
# Cap score at 100
score = min(100, max(0, score))
# Determine strength level
if score >= 80:
level = "Very Strong"
color = Fore.GREEN
elif score >= 60:
level = "Strong"
color = Fore.CYAN
elif score >= 40:
level = "Medium"
color = Fore.YELLOW
else:
level = "Weak"
color = Fore.RED
return {
"score": score,
"level": level,
"color": color,
"feedback": feedback
}
def display_strength_analysis(password: str):
"""
Show a visual analysis of password strength.
Args:
password: The password to analyze
"""
analysis = check_password_strength(password)
print(f"\n{Fore.CYAN}{'='*60}")
print(f"{Fore.CYAN}Password Strength Analysis")
print(f"{Fore.CYAN}{'='*60}")
print(f"Strength: {analysis['color']}{analysis['level']} ({analysis['score']}/100){Style.RESET_ALL}")
# Visual strength bar
bar_length = 30
filled = int((analysis['score'] / 100) * bar_length)
bar = "█" * filled + "░" * (bar_length - filled)
print(f"Progress: {analysis['color']}{bar}{Style.RESET_ALL}")
if analysis['feedback']:
print(f"\n{Fore.YELLOW}💡 Suggestions to improve:")
for suggestion in analysis['feedback']:
print(f" • {suggestion}")
else:
print(f"\n{Fore.GREEN}✓ Excellent password!")
print(f"{Fore.CYAN}{'='*60}\n")
# ============================================================================
# PASSWORD GENERATOR
# ============================================================================
def generate_password(length=16, use_uppercase=True, use_lowercase=True,
use_digits=True, use_symbols=True) -> str:
"""
Generate a cryptographically secure random password.
Uses secrets module (not random!) for cryptographic security.
The secrets module uses the OS's cryptographically secure random
number generator, making passwords unpredictable.
Args:
length: Password length (8-64 characters)
use_uppercase: Include A-Z
use_lowercase: Include a-z
use_digits: Include 0-9
use_symbols: Include !@#$%^&* etc.
Returns:
A strong random password
"""
# Build the character pool and guarantee at least one char from each enabled type
char_pool = ""
required = []
if use_lowercase:
char_pool += string.ascii_lowercase
required.append(secrets.choice(string.ascii_lowercase))
if use_uppercase:
char_pool += string.ascii_uppercase
required.append(secrets.choice(string.ascii_uppercase))
if use_digits:
char_pool += string.digits
required.append(secrets.choice(string.digits))
if use_symbols:
char_pool += string.punctuation
required.append(secrets.choice(string.punctuation))
if not char_pool:
# Fallback if user disabled everything
char_pool = string.ascii_letters + string.digits
# Fill remaining slots then shuffle securely
remaining = [secrets.choice(char_pool) for _ in range(length - len(required))]
password_chars = required + remaining
secrets.SystemRandom().shuffle(password_chars)
return ''.join(password_chars)
def password_generator_wizard():
"""
Interactive wizard to generate a custom password.
Returns:
Generated password, or None if cancelled
"""
print(f"\n{Fore.CYAN}{'='*60}")
print(f"{Fore.CYAN}🔐 Password Generator")
print(f"{Fore.CYAN}{'='*60}\n")
# Get desired length
while True:
try:
length_input = input(f"Password length (8-64) [{Fore.GREEN}16{Style.RESET_ALL}]: ").strip()
if not length_input:
length = 16
else:
length = int(length_input)
if 8 <= length <= 64:
break
else:
print(f"{Fore.RED}Please enter a number between 8 and 64")
except ValueError:
print(f"{Fore.RED}Please enter a valid number")
# Get character preferences
print(f"\n{Fore.CYAN}Include in password:")
use_uppercase = input(f" Uppercase letters (A-Z)? [{Fore.GREEN}Y{Style.RESET_ALL}/n]: ").strip().lower() != 'n'
use_lowercase = input(f" Lowercase letters (a-z)? [{Fore.GREEN}Y{Style.RESET_ALL}/n]: ").strip().lower() != 'n'
use_digits = input(f" Numbers (0-9)? [{Fore.GREEN}Y{Style.RESET_ALL}/n]: ").strip().lower() != 'n'
use_symbols = input(f" Symbols (!@#$%^&*)? [{Fore.GREEN}Y{Style.RESET_ALL}/n]: ").strip().lower() != 'n'
# Generate and optionally regenerate without recursion
while True:
password = generate_password(length, use_uppercase, use_lowercase, use_digits, use_symbols)
print(f"\n{Fore.GREEN}✓ Generated Password:")
print(f"{Fore.WHITE}{Style.BRIGHT}{password}{Style.RESET_ALL}")
display_strength_analysis(password)
choice = input(f"Use this password? [{Fore.GREEN}Y{Style.RESET_ALL}/n/r=regenerate]: ").strip().lower()
if choice == 'r':
continue
elif choice == 'n':
return None
else:
return password
# ============================================================================
# PASSWORD STORAGE & MANAGEMENT
# ============================================================================
def load_passwords(key: bytes) -> dict:
"""
Load and decrypt all passwords from disk.
Args:
key: Encryption key from master password
Returns:
Dictionary with structure:
{
"passwords": [
{
"id": unique_id,
"site": "example.com",
"username": "user@email.com",
"password": "secret123",
"category": "Email",
"notes": "Optional notes",
"created": "2024-01-01 12:00:00",
"modified": "2024-01-01 12:00:00"
},
...
]
}
"""
if not PASSWORDS_FILE.exists():
return {"passwords": []}
try:
encrypted_data = PASSWORDS_FILE.read_bytes()
return decrypt_data(encrypted_data, key)
except InvalidToken as exc:
raise ValueError("Invalid master password or corrupted data file") from exc
def save_passwords(data: dict, key: bytes):
"""
Encrypt and save all passwords to disk.
Args:
data: Complete password database
key: Encryption key from master password
"""
encrypted_data = encrypt_data(data, key)
PASSWORDS_FILE.write_bytes(encrypted_data)
def add_password(data: dict, key: bytes):
"""
Add a new password entry with full details.
Args:
data: Current password database
key: Encryption key (for saving)
"""
print(f"\n{Fore.CYAN}{'='*60}")
print(f"{Fore.CYAN}➕ Add New Password")
print(f"{Fore.CYAN}{'='*60}\n")
# Get site/service name
site = input("Site/Service name (e.g., Gmail, Facebook): ").strip()
if not site:
print(f"{Fore.RED}Site name is required")
return
# Get username/email
username = input("Username/Email: ").strip()
if not username:
print(f"{Fore.RED}Username is required")
return
# Get or generate password
print(f"\n{Fore.CYAN}Password Options:")
print(" 1. Enter password manually")
print(" 2. Generate strong password")
choice = input("Choose option [1-2]: ").strip()
if choice == '2':
password = password_generator_wizard()
if password is None:
print(f"{Fore.YELLOW}Cancelled")
return
else:
password = getpass.getpass("Password (hidden): ")
if not password:
print(f"{Fore.RED}Password is required")
return
# Show strength analysis for manually entered passwords
display_strength_analysis(password)
confirm = input(f"Continue with this password? [{Fore.GREEN}Y{Style.RESET_ALL}/n]: ").strip().lower()
if confirm == 'n':
return
# Select category
print(f"\n{Fore.CYAN}Select Category:")
for i, cat in enumerate(CATEGORIES, 1):
print(f" {i}. {cat}")
try:
cat_choice = input(f"Category [1-{len(CATEGORIES)}]: ").strip()
category = CATEGORIES[int(cat_choice) - 1]
except (ValueError, IndexError):
category = "Other"
print(f"{Fore.YELLOW}Invalid choice, using 'Other'")
# Optional notes
notes = input("Notes (optional, press Enter to skip): ").strip()
# Create new entry
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
new_entry = {
"id": secrets.token_hex(8), # Unique 16-character hex ID
"site": site,
"username": username,
"password": password,
"category": category,
"notes": notes,
"created": now,
"modified": now
}
data["passwords"].append(new_entry)
save_passwords(data, key)
print(f"\n{Fore.GREEN}✓ Password saved successfully!")
print(f"{Fore.GREEN} ID: {new_entry['id']}")
def view_passwords(data: dict, category_filter=None):
"""
Display all passwords (or filtered by category) in a nice table.
Args:
data: Password database
category_filter: Optional category to filter by
"""
passwords = data["passwords"]
if category_filter:
passwords = [p for p in passwords if p["category"] == category_filter]
header = f"Passwords in Category: {category_filter}"
else:
header = "All Passwords"
if not passwords:
print(f"\n{Fore.YELLOW}No passwords found")
return
print(f"\n{Fore.CYAN}{'='*60}")
print(f"{Fore.CYAN}{header} ({len(passwords)} entries)")
print(f"{Fore.CYAN}{'='*60}\n")
if TABULATE_ENABLED:
# Beautiful table format
table_data = []
for p in passwords:
table_data.append([
p['id'][:8] + "...", # Shortened ID
p['site'],
p['username'],
p['category'],
p['modified'][:10] # Just the date
])
print(tabulate(
table_data,
headers=["ID", "Site", "Username", "Category", "Modified"],
tablefmt="grid"
))
else:
# Fallback simple format
for p in passwords:
print(f"{Fore.WHITE}{Style.BRIGHT}{p['site']}{Style.RESET_ALL}")
print(f" ID: {p['id']}")
print(f" Username: {p['username']}")
print(f" Category: {p['category']}")
print(f" Modified: {p['modified']}")
print()
def search_and_show_password(data: dict):
"""
Search for a password and show its full details including the actual password.
Args:
data: Password database
"""
search_term = input(f"\n{Fore.CYAN}Search (site name or username): ").strip().lower()
if not search_term:
return
# Find matching passwords
matches = [
p for p in data["passwords"]
if search_term in p['site'].lower() or search_term in p['username'].lower()
]
if not matches:
print(f"{Fore.YELLOW}No matches found")
return
if len(matches) == 1:
show_password_details(matches[0])
else:
# Multiple matches - let user choose
print(f"\n{Fore.CYAN}Found {len(matches)} matches:")
for i, p in enumerate(matches, 1):
print(f" {i}. {p['site']} ({p['username']}) - {p['category']}")
try:
choice = int(input(f"\nSelect [1-{len(matches)}]: ")) - 1
show_password_details(matches[choice])
except (ValueError, IndexError):
print(f"{Fore.RED}Invalid selection")
def show_password_details(entry: dict):
"""
Show complete details of a password entry including the actual password.
Args:
entry: Single password entry dict
"""
print(f"\n{Fore.CYAN}{'='*60}")
print(f"{Fore.CYAN}Password Details")
print(f"{Fore.CYAN}{'='*60}")
print(f"ID: {entry['id']}")
print(f"Site: {Fore.WHITE}{Style.BRIGHT}{entry['site']}{Style.RESET_ALL}")
print(f"Username: {entry['username']}")
print(f"Password: {Fore.GREEN}{Style.BRIGHT}{entry['password']}{Style.RESET_ALL}")
print(f"Category: {entry['category']}")
if entry.get('notes'):
print(f"Notes: {entry['notes']}")
print(f"Created: {entry['created']}")
print(f"Modified: {entry['modified']}")
print(f"{Fore.CYAN}{'='*60}\n")
def update_password(data: dict, key: bytes):
"""
Update an existing password entry.
Args:
data: Password database
key: Encryption key (for saving)
"""
search_term = input(f"\n{Fore.CYAN}Search for entry to update: ").strip().lower()
matches = [
p for p in data["passwords"]
if search_term in p['site'].lower() or search_term in p['username'].lower() or search_term in p['id'].lower()
]
if not matches:
print(f"{Fore.YELLOW}No matches found")
return
if len(matches) > 1:
print(f"\n{Fore.CYAN}Multiple matches found:")
for i, p in enumerate(matches, 1):
print(f" {i}. {p['site']} ({p['username']})")
try:
choice = int(input(f"Select [1-{len(matches)}]: ")) - 1
entry = matches[choice]
except (ValueError, IndexError):
print(f"{Fore.RED}Invalid selection")
return
else:
entry = matches[0]
# Show current details
show_password_details(entry)
print(f"{Fore.CYAN}What to update? (press Enter to keep current value)")
# Update site
new_site = input(f"Site [{entry['site']}]: ").strip()
if new_site:
entry['site'] = new_site
# Update username
new_username = input(f"Username [{entry['username']}]: ").strip()
if new_username:
entry['username'] = new_username
# Update password
change_pw = input("Change password? (y/N): ").strip().lower()
if change_pw == 'y':
print(f"\n{Fore.CYAN}Password Options:")
print(" 1. Enter new password manually")
print(" 2. Generate strong password")
choice = input("Choose [1-2]: ").strip()
if choice == '2':
new_password = password_generator_wizard()
if new_password:
entry['password'] = new_password
else:
new_password = getpass.getpass("New password (hidden): ")
if new_password:
entry['password'] = new_password
display_strength_analysis(new_password)
# Update category
print(f"\nCurrent category: {entry['category']}")
print(f"{Fore.CYAN}Categories:")
for i, cat in enumerate(CATEGORIES, 1):
print(f" {i}. {cat}")
cat_input = input("New category [press Enter to keep current]: ").strip()
if cat_input:
try:
entry['category'] = CATEGORIES[int(cat_input) - 1]
except (ValueError, IndexError):
pass
# Update notes ('-' clears existing notes)
new_notes = input(f"Notes [{entry.get('notes', '')}] (press Enter to keep, '-' to clear): ").strip()
if new_notes == '-':
entry['notes'] = ''
elif new_notes:
entry['notes'] = new_notes
# Update modification time
entry['modified'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
save_passwords(data, key)
print(f"\n{Fore.GREEN}✓ Password updated successfully!")
def delete_password(data: dict, key: bytes):
"""
Delete a password entry with confirmation.
Args:
data: Password database
key: Encryption key (for saving)
"""
search_term = input(f"\n{Fore.CYAN}Search for entry to delete: ").strip().lower()
matches = [
p for p in data["passwords"]
if search_term in p['site'].lower() or search_term in p['username'].lower() or search_term in p['id'].lower()
]
if not matches:
print(f"{Fore.YELLOW}No matches found")
return
if len(matches) > 1:
print(f"\n{Fore.CYAN}Multiple matches found:")
for i, p in enumerate(matches, 1):
print(f" {i}. {p['site']} ({p['username']})")
try:
choice = int(input(f"Select [1-{len(matches)}]: ")) - 1
entry = matches[choice]
except (ValueError, IndexError):
print(f"{Fore.RED}Invalid selection")
return
else:
entry = matches[0]
# Show what will be deleted
show_password_details(entry)
# Confirm deletion
confirm = input(f"{Fore.RED}Delete this entry? Type 'DELETE' to confirm: ").strip()
if confirm == 'DELETE':
data["passwords"] = [p for p in data["passwords"] if p['id'] != entry['id']]
save_passwords(data, key)
print(f"\n{Fore.GREEN}✓ Password deleted successfully")
else:
print(f"{Fore.YELLOW}Deletion cancelled")
# ============================================================================
# CATEGORY & STATISTICS
# ============================================================================
def view_by_category(data: dict):
"""
Show passwords organized by category.
Args:
data: Password database
"""
print(f"\n{Fore.CYAN}{'='*60}")
print(f"{Fore.CYAN}📁 View by Category")
print(f"{Fore.CYAN}{'='*60}\n")
# Count passwords per category
category_counts = {}
for cat in CATEGORIES:
count = len([p for p in data["passwords"] if p["category"] == cat])
if count > 0:
category_counts[cat] = count
if not category_counts:
print(f"{Fore.YELLOW}No passwords stored yet")
return
print("Categories:")
for i, (cat, count) in enumerate(category_counts.items(), 1):
print(f" {i}. {cat} ({count} passwords)")
print(" 0. View all categories statistics")
choice = input(f"\nSelect category [0-{len(category_counts)}]: ").strip()
if choice == '0':
show_statistics(data)
else:
try:
cat_name = list(category_counts.keys())[int(choice) - 1]
view_passwords(data, cat_name)
except (ValueError, IndexError):
print(f"{Fore.RED}Invalid selection")
def show_statistics(data: dict):
"""
Display statistics about stored passwords.
Args:
data: Password database
"""
passwords = data["passwords"]
total = len(passwords)
print(f"\n{Fore.CYAN}{'='*60}")
print(f"{Fore.CYAN}📊 Password Manager Statistics")
print(f"{Fore.CYAN}{'='*60}\n")
print(f"Total Passwords: {Fore.WHITE}{Style.BRIGHT}{total}{Style.RESET_ALL}")
if total == 0:
return
# Category breakdown
print(f"\n{Fore.CYAN}By Category:")
category_counts = {}
for cat in CATEGORIES:
count = len([p for p in passwords if p["category"] == cat])
if count > 0:
category_counts[cat] = count
percentage = (count / total) * 100
bar = "█" * int(percentage / 5)
print(f" {cat:15} {count:3} ({percentage:5.1f}%) {bar}")
# Strength analysis
print(f"\n{Fore.CYAN}Password Strength Overview:")
weak = medium = strong = very_strong = 0
for p in passwords:
analysis = check_password_strength(p['password'])
if analysis['score'] >= 80:
very_strong += 1
elif analysis['score'] >= 60:
strong += 1
elif analysis['score'] >= 40:
medium += 1
else:
weak += 1
if weak > 0:
print(f" {Fore.RED}Weak: {weak}")
if medium > 0:
print(f" {Fore.YELLOW}Medium: {medium}")
if strong > 0:
print(f" {Fore.CYAN}Strong: {strong}")
if very_strong > 0:
print(f" {Fore.GREEN}Very Strong: {very_strong}")
if weak > 0:
print(f"\n{Fore.YELLOW}💡 Tip: You have {weak} weak password(s). Consider updating them!")
print()
# ============================================================================
# BACKUP & RESTORE
# ============================================================================
def create_backup(data: dict, key: bytes):
"""
Create an encrypted backup with timestamp.
Args:
data: Password database
key: Encryption key
"""
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
backup_file = BACKUP_DIR / f"passwords_backup_{timestamp}.enc"
# Create encrypted backup
encrypted_data = encrypt_data(data, key)
backup_file.write_bytes(encrypted_data)
print(f"\n{Fore.GREEN}✓ Backup created successfully!")
print(f"{Fore.GREEN} Location: {backup_file}")
print(f"{Fore.GREEN} Size: {len(encrypted_data)} bytes")
# Show existing backups
list_backups()
def list_backups():
"""
List all available backups.
"""
backups = sorted(BACKUP_DIR.glob("passwords_backup_*.enc"), reverse=True)
if not backups:
print(f"\n{Fore.YELLOW}No backups found")
return
print(f"\n{Fore.CYAN}Available Backups:")
for i, backup in enumerate(backups, 1):
size_kb = backup.stat().st_size / 1024
modified = datetime.fromtimestamp(backup.stat().st_mtime)
print(f" {i}. {backup.name}")
print(f" Created: {modified.strftime('%Y-%m-%d %H:%M:%S')}")
print(f" Size: {size_kb:.1f} KB")
def restore_from_backup(key: bytes) -> dict | None:
"""
Restore password database from a backup file.
Args:
key: Encryption key
Returns:
Restored password database, or None if cancelled
"""
backups = sorted(BACKUP_DIR.glob("passwords_backup_*.enc"), reverse=True)
if not backups:
print(f"\n{Fore.YELLOW}No backups found")
return None
print(f"\n{Fore.CYAN}{'='*60}")
print(f"{Fore.CYAN}📦 Restore from Backup")
print(f"{Fore.CYAN}{'='*60}\n")
print(f"{Fore.CYAN}Available Backups:")
for i, backup in enumerate(backups, 1):
modified = datetime.fromtimestamp(backup.stat().st_mtime)
print(f" {i}. {backup.name}")
print(f" Created: {modified.strftime('%Y-%m-%d %H:%M:%S')}")
try:
choice = int(input(f"\nSelect backup [1-{len(backups)}, 0 to cancel]: "))
if choice == 0:
return None
backup_file = backups[choice - 1]
# Confirm restoration
print(f"\n{Fore.RED}⚠️ WARNING: This will replace your current passwords!")
confirm = input("Type 'RESTORE' to confirm: ").strip()
if confirm != 'RESTORE':
print(f"{Fore.YELLOW}Restoration cancelled")
return None
# Load and decrypt backup
encrypted_data = backup_file.read_bytes()
restored_data = decrypt_data(encrypted_data, key)
print(f"\n{Fore.GREEN}✓ Backup restored successfully!")
print(f"{Fore.GREEN} Restored {len(restored_data['passwords'])} passwords")