How do I convert a password into asterisks while it is being entered? [duplicate]
Asked Answered
C

10

32

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.

I am not asking about how to disable echoing characters to the screen, which is what Python's getpass.getpass() module does. I specifically want the feature where asterisks appear in response to key presses, which getpass doesn't do.

Committeeman answered 4/3, 2016 at 20:2 Comment(2)
This is pretty vague, to me - what is your source of input? A web page? Terminal? A tkinter text area? Yes to all, but it's different for eachDollar
I was actually doing this for a school project and i'm a very basic programmer.... but i'll try to use the tkinter text area so if u could help with that it'd be greatCommitteeman
B
24

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/

Bathsheba answered 30/10, 2020 at 1:34 Comment(2)
I'm not sure what you mean by "getpass is Linux and macOS only". Python's documentation describes getpass as "Portable password input". While it might not reliably disable echoing on all platforms, it should always be available, falling back to reading from stdin if necessary.Philately
Ah, you're right. I was thinking of another module other than getpass. The getpass module doesn't and has never displayed the asterisks.Bathsheba
P
43

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

import getpass

password = getpass.getpass()
print(password)
Penthea answered 4/3, 2016 at 20:4 Comment(3)
yes but do you know something that could display (*) instead of nothing while typing the password?Donahoe
This hides the password entirely. It does not display asterisks (*).Amphithecium
Not true today, in Python 3.7, using a Jupyter lab cell. I get bullet points (or are those very small asterisks?)!Archpriest
B
24

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/

Bathsheba answered 30/10, 2020 at 1:34 Comment(2)
I'm not sure what you mean by "getpass is Linux and macOS only". Python's documentation describes getpass as "Portable password input". While it might not reliably disable echoing on all platforms, it should always be available, falling back to reading from stdin if necessary.Philately
Ah, you're right. I was thinking of another module other than getpass. The getpass module doesn't and has never displayed the asterisks.Bathsheba
F
11

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.

Faustofaustus answered 4/3, 2016 at 20:59 Comment(0)
C
3

You can do this:

# if getch module is available, then we implement our own getpass() with asterisks,
# otherwise we just use the plain boring getpass.getpass()
try:
    import getch
    def getpass(prompt):
        """Replacement for getpass.getpass() which prints asterisks for each character typed"""
        print(prompt, end='', flush=True)
        buf = ''
        while True:
            ch = getch.getch()
            if ch == '\n':
                print('')
                break
            else:
                buf += ch
                print('*', end='', flush=True)
        return buf
except ImportError:
    from getpass import getpass
Criminate answered 3/10, 2018 at 22:18 Comment(1)
May want to handle backspaces and Ctrl + UChipper
B
3

first install the library:

pip install stdiomask

note that you install std io mask not studiomask...

And then the code is:

password = stdiomask.getpass() # It will ask to enter password and display * on the screen

print(password)

This is the output:

Password: *****
google
Beloved answered 20/11, 2020 at 7:50 Comment(1)
Minor note: the stdiomask module is now called pwinput. See the other answer in this question for more info.Bathsheba
V
2

while using getpass in python, nothing is indicated to show a password input.

this can be resolved by this simple solution:

just copy the ‘getpass_ak.py’ module provided in the link to python’s Lib folder.

https://starrernet.wixsite.com/analytix/python-coder

use the following code:

import getpass_ak

a = (getpass_ak.getpass('password: '))

this will add * to your password inputs.

Visigoth answered 11/1, 2020 at 18:46 Comment(0)
B
2

For anyone who would actually want to have asterisks appear, here's an improvement on Tigran Aivazian's answer. This version imports the built-in msvcrt.getch, adds cases for different line endings when hitting 'Enter/Return', and includes logic to support Backspace, as well as Ctrl+C (KeyboardInterrupt):

try:
    from msvcrt import getch
    def getpass(prompt):
        """Replacement for getpass.getpass() which prints asterisks for each character typed"""
        print(prompt, end='', flush=True)
        buf = b''
        while True:
            ch = getch()
            if ch in {b'\n', b'\r', b'\r\n'}:
                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': # Ctrl+C
                raise KeyboardInterrupt
            else:
                buf += ch
                print('*', end='', flush=True)
        return buf.decode(encoding='utf-8')
except ImportError:
    from getpass import getpass

Please feel free to suggest any other changes, or ways to improve this; I hacked the changes together pretty quickly, especially with the Backspace logic.

Boudicca answered 13/2, 2020 at 15:51 Comment(0)
M
1

I have combined the answers of @Tigran Aivazian and @Ahndwoo into fully working solution:

  • ! additional code for the backspace {b'\x08', b'\x7f'}: # Backspace is added
  • for the Ctrl+C combination the silent return is used. The raise KeyboardInterrupt is commented now, but can be uncommented for raise the error.
# if getch module is available, then we implement our own getpass() with asterisks,
# otherwise we just use the plain boring getpass.getpass()
try:
    from getch import getch
    def getpass(prompt):
        """Replacement for getpass.getpass() which prints asterisks for each character typed"""
        print(prompt, end='', flush=True)
        buf = b''
        while True:
            ch = getch().encode()
            if ch in {b'\n', b'\r', b'\r\n'}:
                print('')
                break
            elif ch == b'\x03': # Ctrl+C
                # raise KeyboardInterrupt
                return ''
            elif ch in {b'\x08', b'\x7f'}: # Backspace
                buf = buf[:-1]
                print(f'\r{(len(prompt)+len(buf)+1)*" "}\r{prompt}{"*" * len(buf)}', end='', flush=True)
            else:
                buf += ch
                print('*', end='', flush=True)

        return buf.decode(encoding='utf-8')
except ImportError:
    from getpass import getpass

password = getpass('Enter password: ')
print(password)
Margeret answered 25/10, 2020 at 16:28 Comment(1)
Very nice start, as a password helper written in python. Evolution is almost identical to something I wrote in shell. antofthy.gitlab.io/software/#askpass_starsVenturesome
V
1

My own suggestion is -- DO NOT DO IT!

Don't reinvent the wheel, use a password helper if you want 'stars'

Instead do something like the following pseudo-code... If the TTY_ASSPASS environment variable is set -- call that password helper. There are a number available, including "systemd-ask-password" If there is a TTY - fall back to read no echo (getpass.getpass()) If there is no TTY, - fall back just read STDIN with rstrip()

This not only lets the user select a password input program (via an environment variable (or some other confuguration), but also lets them substitute for other sources of passwords, like GUI input, or collecting password from a pre-opened keyring password daemon, or elsewhere

Don't restrict or otherwise limit where passwords come from!

For notes on password helpers see.. https://antofthy.gitlab.io/info/crypto/passwd_input.txt

For a shell script password helper https://antofthy.gitlab.io/software/#askpass_stars

Looks like Andrei Krivoshei answer has already made a start on that, resulting in something very similar, but still in its infancy.

Venturesome answered 30/4, 2021 at 2:10 Comment(0)
A
0

You may want to check getpass function.

Prompt the user for a password without echoing. The user is prompted using the string prompt, which defaults to 'Password: '. On Unix, the prompt is written to the file-like object stream. stream defaults to the controlling terminal (/dev/tty) or if that is unavailable to sys.stderr (this argument is ignored on Windows).

Note: This module mimics unix password prompts and does not show asterisks.

Usage:

import getpass
getpass.getpass()
Altheta answered 4/3, 2016 at 20:4 Comment(1)
Men, you got beat by a few seconds, did ya? ;)Archpriest

© 2022 - 2024 — McMap. All rights reserved.