-
Notifications
You must be signed in to change notification settings - Fork 1
/
white-password.py
35 lines (32 loc) · 1.32 KB
/
white-password.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
"""
White-Password
Secure-Password-Input
Licensed under CC0-1.0 License to Pavana Narayana Bhat
------------------------------------------------------------------------------------------------------------------------
Author: pixincreate
------------------------------------------------------------------------------------------------------------------------
Description:
You don't want / wish your password to
be known to anyone in this world except you.
If YES, you're at the right place! Grab the
code, re-use it in your python Code.
NOTE: Usage in README.md
"""
from msvcrt import getch
def white_password(prompt): # A simple approach to Secure password input.!
print(prompt, end='', flush=True)
buf = b''
while True:
ch = getch()
if ch in {b'\n', b'\r', b'\r\n'}: # End Of Line or Carriage Return
print('')
break
elif ch == b'\x08': # Backspace
buf = buf[:-1]
print(f'\r{(len(prompt) + len(buf) + 1) * ""}\r{prompt}{"" * len(buf)}', end='', flush=True)
elif ch == b'\x03': # Copy or Ctrl + C
raise KeyboardInterrupt
else:
buf += ch
print('', end='', flush=True) # Prints nothing instead of Password on the screen
return buf.decode(encoding='utf-8')