This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Adding the ability for getpass to print asterisks when password is typed
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Jeffrey.Kintscher, andrei.avk, celal.sahin, jab, matanya.stroh, r.david.murray, remi.lapeyre, steven.daprano, stevoisiak
Priority: normal Keywords:

Created on 2018-02-20 10:28 by matanya.stroh, last changed 2022-04-11 14:58 by admin.

Messages (7)
msg312410 - (view) Author: Matanya Stroh (matanya.stroh) Date: 2018-02-20 10:28
I saw some questions about it in stackoverflow (links below), and also find it very useful to have the ability to print asterisks.
Some users, find it disturbing when they don't have any indication that password is typed, and it will be helpful to have it.

I know that it's have some risks exposing the number of chars to the password, but I think it's worth it.

When using Jupyter (notebook server is 4.3.1) the password does echoed as "*", but not in Python IDE in linux and Windows

1) https://stackoverflow.com/questions/10990998/how-to-have-password-echoed-as-asterisks
2) https://stackoverflow.com/questions/7838564/how-to-read-password-with-echo-in-python-console-program
msg312520 - (view) Author: Matanya Stroh (matanya.stroh) Date: 2018-02-22 00:22
for getpass.win_getpass() it can simply be done by adding this line
msvcrt.putch("*").
So the code will look like:

def win_getpass(prompt='Password: ', stream=None):
    """Prompt for password with echo off, using Windows getch()."""
    if sys.stdin is not sys.__stdin__:
        return fallback_getpass(prompt, stream)

    for c in prompt:
        msvcrt.putwch(c)
    pw = ""
    while 1:
        c = msvcrt.getwch()
        if c == '\r' or c == '\n':
            break
        if c == '\003':
            raise KeyboardInterrupt
        if c == '\b':
            pw = pw[:-1]
        else:
            pw = pw + c
            msvcrt.putch("*") #Line that was added
    msvcrt.putwch('\r')
    msvcrt.putwch('\n')
    return pw
msg312745 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-02-24 19:13
getpass is emulating the unix password prompt behavior.  I'm not sure if the complication is worth it, especially since not echoing asterisks is, as you observe, fractionally more secure.  So I guess I'm about -.5 on this feature.
msg339803 - (view) Author: Steven Vascellaro (stevoisiak) * Date: 2019-04-09 19:00
@matanya.stroh: Don't forget to erase the asterisks if the user hits backspace.

```
def win_getpass(prompt='Password: ', stream=None, show_asterisks=False):
    """Prompt for password with echo off, using Windows getch()."""
    if sys.stdin is not sys.__stdin__:
        return fallback_getpass(prompt, stream)

    for c in prompt:
        msvcrt.putwch(c)
    pw = ""
    while 1:
        c = msvcrt.getwch()
        if c == '\r' or c == '\n':
            break
        if c == '\003':
            raise KeyboardInterrupt
        if c == '\b':
            if len(pw) > 0:
                pw = pw[:-1]
                msvcrt.putwch('\b')
                msvcrt.putwch(' ')
                msvcrt.putwch('\b')
        else:
            pw = pw + c
            if show_asterisks:
                msvcrt.putwch('*')
    msvcrt.putwch('\r')
    msvcrt.putwch('\n')
    return pw
```

Alternatively, could let the user define the masking character, similar to Tkinter's Entry widget.

```
def win_getpass(prompt='Password: ', stream=None, mask=''):
    """Prompt for password with echo off, using Windows getch()."""
    if sys.stdin is not sys.__stdin__:
        return fallback_getpass(prompt, stream)
    if len(mask) > 1:
        raise TypeError('mask argument must be a zero- or one-character str')

    for c in prompt:
        msvcrt.putwch(c)
    pw = ""
    while 1:
        c = msvcrt.getwch()
        if c == '\r' or c == '\n':
            break
        if c == '\003':
            raise KeyboardInterrupt
        if c == '\b':
            if len(pw) > 0:
                pw = pw[:-1]
                msvcrt.putwch('\b')
                msvcrt.putwch(' ')
                msvcrt.putwch('\b')
        else:
            pw = pw + c
            if mask:
                msvcrt.putwch(mask)
    msvcrt.putwch('\r')
    msvcrt.putwch('\n')
    return pw
```

I'm in favor of supporting masking. While it does reveal the password length, it's an accessibility feature many users have come to expect.

I'd rather have this in the standard library than have developers implement their own custom, potentially insecure methods for password input.
msg344784 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-06-06 03:14
See also #36566. (Thanks Cheryl.)

I think the usability improvement for this far outweigh the decrease in security.

The days where somebody looking over your shoulder watching you type your password was the major threat are long gone. Hiding the length of the password against a shoulder-surfing adversary is so-1970s :-)

For old-school Unix types we ought to default to hiding the password. But I'm +1 in allowing developers to choose to trade off a tiny decrease in security against a major increase in usability.

The bottom line is that if you have a weak password, hiding the length won't save you; if you have a strong password, hiding the length doesn't add any appreciable difficulty to the attacker.
msg375038 - (view) Author: Jeffrey Kintscher (Jeffrey.Kintscher) * Date: 2020-08-08 07:33
This is easy to implement for Windows (as shown), but the unix implementation uses io.TextIOWrapper.readline() to get the password input.  The unix implementation would have to be rewritten to provide the same functionality.
msg399298 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-08-10 01:27
Unfortunately modern laptop keyboards have almost no key travel and barely any tactile feedback [*]. Users on such keyboards really do need feedback for each key pressed. Not providing an option for such feedback is in effect forcing users to choose maximally weak password.

[*] worse, a large proportion of MBP keyboards produced in the last few years have the notoriously bad 'butterfly' key design that occasionally duplicates and swallows keypresses. Yes, a trillion dollar company can't make a functional keyboard.
History
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77065
2021-08-10 01:27:06andrei.avksetnosy: + andrei.avk
messages: + msg399298
2020-08-08 07:33:54Jeffrey.Kintschersetmessages: + msg375038
2020-08-08 04:15:56Jeffrey.Kintschersetnosy: + Jeffrey.Kintscher
2020-06-25 00:54:38jabsetnosy: + jab
2019-11-14 12:17:39celal.sahinsetnosy: + celal.sahin
2019-06-10 04:18:00serhiy.storchakasettitle: Adding the ability for getpass to print asterisks when passowrd is typed -> Adding the ability for getpass to print asterisks when password is typed
2019-06-09 16:00:38remi.lapeyresetnosy: + remi.lapeyre
2019-06-06 03:14:56steven.dapranosetnosy: + steven.daprano

messages: + msg344784
versions: + Python 3.9, - Python 3.8
2019-04-09 19:00:04stevoisiaksetnosy: + stevoisiak
messages: + msg339803
2019-04-08 21:05:01cheryl.sabellalinkissue36566 superseder
2018-02-24 19:13:51r.david.murraysetnosy: + r.david.murray
messages: + msg312745
2018-02-22 00:22:45matanya.strohsetmessages: + msg312520
2018-02-20 10:28:53matanya.strohcreate