Masking user input in python with asterisks


Masking user input in python with asterisks



I am trying to mask what the user types into IDLE with asterisks so people around them can't see what they're typing/have typed in. I'm using basic raw input to collect what they type.


key = raw_input('Password :: ')



Ideal IDLE prompt after user types password:


Password :: **********





You will have to do some custom stdout redirection to mask with asterisks, but there is an easier way to get passwords stackoverflow.com/a/1761753/1268926
– Kedar
Dec 24 '14 at 5:01




3 Answers
3



Depending on the OS, how you get a single character from user input and how to check for the carriage return will be different.



See this post: Python read a single character from the user



On OSX, for example, you could so something like this:


import sys, tty, termios

def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch

key = ""
sys.stdout.write('Password :: ')
while True:
ch = getch()
if ch == 'r':
break
key += ch
sys.stdout.write('*')
print
print key



To solve this I wrote this small module pyssword to mask the user input password at the prompt. It works with windows. The code is below:


from msvcrt import getch
import getpass, sys

def pyssword(prompt='Password: '):
'''
Prompt for a password and masks the input.
Returns:
the value entered by the user.
'''

if sys.stdin is not sys.__stdin__:
pwd = getpass.getpass(prompt)
return pwd
else:
pwd = ""
sys.stdout.write(prompt)
sys.stdout.flush()
while True:
key = ord(getch())
if key == 13: #Return Key
sys.stdout.write('n')
return pwd
break
if key == 8: #Backspace key
if len(pwd) > 0:
# Erases previous character.
sys.stdout.write('b' + ' ' + 'b')
sys.stdout.flush()
pwd = pwd[:-1]
else:
# Masks user input.
char = chr(key)
sys.stdout.write('*')
sys.stdout.flush()
pwd = pwd + char





“While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.”
– Ghost
May 25 '16 at 22:43





Code included as requested !
– Rafael Ribeiro
May 26 '16 at 14:12



The below code provides replaces written characters with asterix and allow for deletion of wrongly typed characters. The number of asterixes reflects the number of typed characters.


import getpass
key = getpass.getpass('Password :: ')



enter image description here



And after the user press enter:



enter image description here





Although your code snippet might solve the issue, you should describe what’s the purpose of your code (how it solves the problem). Furthermore, you might want to check stackoverflow.com/help/how-to-answer
– Ahmad F
Mar 7 at 13:31






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Possible Unhandled Promise Rejection (id: 0): ReferenceError: user is not defined ReferenceError: user is not defined

Opening a url is failing in Swift