Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a password into asterisks while it is being entered?

Tags:

python

Is there a way in Python to convert characters as they are being entered by the user to asterisks, like it can be seen on many websites?

For example, if an email user was asked to sign in to their account, while typing in their password, it wouldn't appear as characters but rather as * after each individual stroke without any time lag.

If the actual password was KermitTheFrog, it would appear as ************* when typed in.

like image 206
Sharpfawkes Avatar asked Mar 04 '16 20:03

Sharpfawkes


People also ask

How can I see asterisk password?

See Passwords Behind Asterisk in Google Chrome Open any website where you have your password saved, right-click on the password field and go to Inspect Element. When the HTML Editor opens, look for input type = “password” field and change “password” to “text” and press Enter to save.

How do you censor a password in Python?

In Python with the help of maskpass() module and base64() module we can hide the password of users with asterisk(*) during input time and then with the help of base64() module it can be encrypted.

What is Getpass in Python?

The getpass module provides a platform-independent way to enter a password in a command-line program, as Example 2-25 shows. getpass(prompt) prints the prompt string, switches off keyboard echo, and reads a password. If the prompt argument is omitted, it prints " Password: “.


3 Answers

There is getpass(), a function which hides the user input.

import getpass

password = getpass.getpass()
print(password)
like image 183
pp_ Avatar answered Oct 18 '22 15:10

pp_


If you want a solution that works on Windows/macOS/Linux and on Python 2 & 3, you can install the pwinput module (formerly called stdiomask):

pip install pwinput

Unlike getpass.getpass() (which is in the Python Standard Library), the pwinput module can display *** mask characters as you type. It is also cross-platform, while getpass is Linux and macOS only.

Example usage:

>>> pwinput.pwinput()
Password: *********
'swordfish'
>>> pwinput.pwinput(mask='X') # Change the mask character.
Password: XXXXXXXXX
'swordfish'
>>> pwinput.pwinput(prompt='PW: ', mask='*') # Change the prompt.
PW: *********
'swordfish'
>>> pwinput.pwinput(mask='') # Don't display anything.
Password:
'swordfish'

Unfortunately this module, like Python's built-in getpass module, doesn't work in IDLE or Jupyter Notebook.

More details at https://pypi.org/project/pwinput/

like image 34
Al Sweigart Avatar answered Oct 18 '22 13:10

Al Sweigart


If you're using Tkinter:

# For Python 2:
from Tkinter import Entry, Tk
# For Python 3
from tkinter import Entry, Tk

master = Tk()

Password = Entry(master, bd=5, width=20, show="*")
Password.pack()

master.mainloop()

Password entry with tkinter

In the shell, this is not possible. You can however write a function to store the entered text and report only a string of *'s when called. Kinda like this, which I did not write. I just Googled it.

like image 39
Matt Avatar answered Oct 18 '22 14:10

Matt