Skip to content

Commit 652d078

Browse files
committed
Masked input instead Hidden & more smart_subs addition to Password-Checker.py
1 parent 9cc55ea commit 652d078

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

Password-Checker/check-password.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env python
2-
import re
3-
import sys
4-
import random
2+
import re, sys, random, termios, tty
53
from getpass import getpass
64

75
# ANSI escape codes for colors
@@ -20,7 +18,7 @@
2018
}
2119

2220
COMMON_WORDS = {
23-
'adjectives': ['Happy', 'Clever', 'Swift', 'Brave', 'Bright'],
21+
'adjectives': ['Happy', 'Clever', 'Swift', 'Brave', 'Bright', 'Cool'],
2422
'nouns': ['Tiger', 'River', 'Mountain', 'Storm', 'Star'],
2523
'numbers': ['365', '42', '777', '314', '999'],
2624
'separators': ['_', '.', '#', '*', '@']
@@ -33,6 +31,33 @@
3331
'special': re.compile(r'[!@#$%^&*(),.?":{}|<>]')
3432
}
3533

34+
def passwd_enter(prompt_part, mask="x"):
35+
prompt = f"\nEnter the Password to {prompt_part}: "
36+
sys.stdout.write(prompt)
37+
sys.stdout.flush()
38+
password = ""
39+
fd = sys.stdin.fileno()
40+
old_settings = termios.tcgetattr(fd)
41+
try:
42+
tty.setraw(fd)
43+
while True:
44+
ch = sys.stdin.read(1)
45+
if ch in ('\r', '\n'):
46+
sys.stdout.write('\r\n')
47+
sys.stdout.flush()
48+
break
49+
elif ch == '\x7f': # backspace
50+
if password:
51+
password = password[:-1]
52+
sys.stdout.write('\b \b')
53+
sys.stdout.flush()
54+
else:
55+
password += ch
56+
sys.stdout.write(mask)
57+
sys.stdout.flush()
58+
finally:
59+
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
60+
return password
3661

3762
def format_to_header(
3863
msg: str,
@@ -168,8 +193,9 @@ def suggest_better_password(password):
168193

169194
# Smart character substitutions (maintain readability)
170195
smart_subs = {
171-
'a': '@', 'e': '3', 'i': '!', 'o': '0', 's': '$',
172-
'ate': '8', 'to': '2', 'for': '4'
196+
'a': '@', 'e': '€', 'i': '!', 'o': '0', 's': '$',
197+
'ate': '8', 'to': '2', 'for': '4', 'and': '&',
198+
'b': 'be', 'u': 'you'
173199
}
174200

175201
# Apply substitutions intelligently
@@ -212,7 +238,7 @@ def input_handler():
212238
print("For enhanced security, your input will be hidden.")
213239
print("Hence, you may not see the characters as you type.")
214240
try:
215-
password = getpass("\nEnter password to check: ")
241+
password = passwd_enter("check")
216242
except KeyboardInterrupt:
217243
print("\nExiting...")
218244
sys.exit(0)

0 commit comments

Comments
 (0)