2

All in the title really...

I know you can use

getpass.getpass()

however, if distributing some code like this, people are used to having their characters by '*' so could give the impression that may make them believe their keyboard isn't working when they see no characters popping up on the screen.

I want to have 'example' show up as '*******' during the input, so that:

Enter Password: example

will show up as

Enter Password: *******

Thanks for any answers

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
NightShade
  • 422
  • 5
  • 19
  • You can change the prompt from getpass, e.g. getpass.getpass(prompt='Enter password (NB you receive no indication that a character was entered from e.g. an asterisk):') – innisfree May 04 '17 at 08:18

3 Answers3

2

I have a work around for you, which you can modify to your needs:

  1. Install getch: pip install getch. This module has methods for getting input char by char.
  2. Create a function that gets user input char by char and prints * in it's place:

    #import sys (if option 1. is used)
    import getch
    
    def get_password():
        p = ''
        typed = ''
    
        while True:
            typed = getch.getch()
    
            if typed == '\n':
                print(typed)
                break
    
            p += typed
    
            # Choose one of the following solutions:
    
            # 1. General way. Needs import sys
            sys.stdout.write('*')
            sys.stdout.flush()
    
            # 2. Python 3 way:
            print('*', end='', flush=True)
    
       return p
    

Good luck :)


EDIT: For @timgeb 's comment about security:

From getpass documentation:

On Unix, the prompt is written to the file-like object stream using the replace error handler if needed.
stream defaults to the controlling terminal (/dev/tty) or if that is unavailable to sys.stderr (this argument is ignored on Windows).

So it behaves quite similarly with the function above, except that mine has no fallback option...

John Moutafis
  • 22,254
  • 11
  • 68
  • 112
  • 2
    Can anybody judge the safety of this compared to using the `getpass` module? – timgeb May 04 '17 at 08:06
  • I can't seem to install the getch module on Windows. What should I do? – NightShade May 04 '17 at 08:31
  • In windows install `msvcrt` and use it as `msvcrt.getch()` – John Moutafis May 04 '17 at 08:32
  • `Traceback (most recent call last): File "", line 1, in get_password() File "C:\Users\Default\Desktop\kjhgh.py", line 15, in get_password p += typed TypeError: must be str, not bytes` – NightShade May 04 '17 at 08:37
  • See http://stackoverflow.com/questions/21689365/python-3-typeerror-must-be-str-not-bytes-with-sys-stdout-write?answertab=votes#tab-top also since you are using Python 3, you can use the `print()` option instead! – John Moutafis May 04 '17 at 08:40
1

This one should do it:

import msvcrt
import sys

print('Enter your password: ')
password = ''
while True:
    pressedKey = msvcrt.getch()
    if pressedKey == '\r':    
       break
    else:
        password = password+pressedKey
        sys.stdout.write('*')

print('\n' + password)

It is MS VC++ - Microsoft Visual C++ that works for windows. getpass uses this module, according to docs

zipa
  • 27,316
  • 6
  • 40
  • 58
0

Really stop re-inventing the wheel. DO NOT include something like this in your program, but have it use a password helper program.

See https://stackoverflow.com/a/67327327/701532

Of course if you write a GREAT password helper for inputing stars in python, like I did many years ago in shell... Go for it!

anthony
  • 7,696
  • 1
  • 17
  • 11