Getting a hidden password input
Question:
You know how in Linux when you try some Sudo stuff it tells you to enter the password and, as you type, nothing is shown in the terminal window (the password is not shown)?
Is there a way to do that in Python? I’m working on a script that requires so sensitive info and would like for it to be hidden when I’m typing it.
In other words, I want to get the password from the user without showing the password.
Answers:
Use getpass.getpass()
:
from getpass import getpass
password = getpass()
An optional prompt can be passed as parameter; the default is "Password: "
.
Note that this function requires a proper terminal, so it can turn off echoing of typed characters – see “GetPassWarning: Can not control echo on the terminal” when running from IDLE for further details.
import getpass
pswd = getpass.getpass('Password:')
getpass works on Linux, Windows, and Mac.
This code will print an asterisk instead of every letter.
import sys
import msvcrt
passwor = ''
while True:
x = msvcrt.getch()
if x == 'r':
break
sys.stdout.write('*')
passwor +=x
print 'n'+passwor
Updating on the answer of @Ahmed ALaa
# import msvcrt
import getch
def getPass():
passwor = ''
while True:
x = getch.getch()
# x = msvcrt.getch().decode("utf-8")
if x == 'r' or x == 'n':
break
print('*', end='', flush=True)
passwor +=x
return passwor
print("nout=", getPass())
msvcrt us only for windows, but getch from PyPI should work for both (I only tested with linux).
You can also comment/uncomment the two lines to make it work for windows.
Here is my code based off the code offered by @Ahmed ALaa
Features:
- Works for passwords up to 64 characters
- Accepts backspace input
- Outputs
*
character (DEC: 42 ; HEX: 0x2A)
instead of the input character
Demerits:
- Works on Windows only
The function secure_password_input()
returns the password as a string
when called. It accepts a Password Prompt string, which will be displayed to the user to type the password
def secure_password_input(prompt=''):
p_s = ''
proxy_string = [' '] * 64
while True:
sys.stdout.write('x0D' + prompt + ''.join(proxy_string))
c = msvcrt.getch()
if c == b'r':
break
elif c == b'x08':
p_s = p_s[:-1]
proxy_string[len(p_s)] = " "
else:
proxy_string[len(p_s)] = "*"
p_s += c.decode()
sys.stdout.write('n')
return p_s
You can also use the pwinput module which works on both Windows and Linux.
It replaces the char with ‘*’ (by default) and backspace works.
import pwinput
password = pwinput.pwinput(prompt='Password: ')
You can, optionally, pass a different mask
character as well.
import pwinput
password = pwinput.pwinput(prompt='Password: ', mask='')
See the pwinput documentation for more information.
You know how in Linux when you try some Sudo stuff it tells you to enter the password and, as you type, nothing is shown in the terminal window (the password is not shown)?
Is there a way to do that in Python? I’m working on a script that requires so sensitive info and would like for it to be hidden when I’m typing it.
In other words, I want to get the password from the user without showing the password.
Use getpass.getpass()
:
from getpass import getpass
password = getpass()
An optional prompt can be passed as parameter; the default is "Password: "
.
Note that this function requires a proper terminal, so it can turn off echoing of typed characters – see “GetPassWarning: Can not control echo on the terminal” when running from IDLE for further details.
import getpass
pswd = getpass.getpass('Password:')
getpass works on Linux, Windows, and Mac.
This code will print an asterisk instead of every letter.
import sys
import msvcrt
passwor = ''
while True:
x = msvcrt.getch()
if x == 'r':
break
sys.stdout.write('*')
passwor +=x
print 'n'+passwor
Updating on the answer of @Ahmed ALaa
# import msvcrt
import getch
def getPass():
passwor = ''
while True:
x = getch.getch()
# x = msvcrt.getch().decode("utf-8")
if x == 'r' or x == 'n':
break
print('*', end='', flush=True)
passwor +=x
return passwor
print("nout=", getPass())
msvcrt us only for windows, but getch from PyPI should work for both (I only tested with linux).
You can also comment/uncomment the two lines to make it work for windows.
Here is my code based off the code offered by @Ahmed ALaa
Features:
- Works for passwords up to 64 characters
- Accepts backspace input
- Outputs
*
character(DEC: 42 ; HEX: 0x2A)
instead of the input character
Demerits:
- Works on Windows only
The function secure_password_input()
returns the password as a string
when called. It accepts a Password Prompt string, which will be displayed to the user to type the password
def secure_password_input(prompt=''):
p_s = ''
proxy_string = [' '] * 64
while True:
sys.stdout.write('x0D' + prompt + ''.join(proxy_string))
c = msvcrt.getch()
if c == b'r':
break
elif c == b'x08':
p_s = p_s[:-1]
proxy_string[len(p_s)] = " "
else:
proxy_string[len(p_s)] = "*"
p_s += c.decode()
sys.stdout.write('n')
return p_s
You can also use the pwinput module which works on both Windows and Linux.
It replaces the char with ‘*’ (by default) and backspace works.
import pwinput
password = pwinput.pwinput(prompt='Password: ')
You can, optionally, pass a different mask
character as well.
import pwinput
password = pwinput.pwinput(prompt='Password: ', mask='')
See the pwinput documentation for more information.