Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a password input be done in python with printing an asterisk for every character of the user?

The problem: Programmers want users to input passwords. The getpass() function is nice for this purpose, but its use has a drawback: While entering a password nothing is printed to stdout.

The question: How can a getpass() be implemented while asterisks are printed for every character typed by a user? (Of course backspace - and ideally pos1 and end - should be taken care of accordingly.)

The motivation: There have been people in the community not understanding why this question has been asked. And then referred to getpass() with a) this way ignoring the task at hand and b) without thinking about that there reference would not answer the question. The reason why s.o. might want to have asterisks printed is for convenience of users: They get a direct visual response during password input. Therefor they do not get confused by pressing keys and - to there eyes - nothing seems to be happening.

A step towards a solution:

Let me present a first step towards a solution here. Please help in order to evolve it into a real solution.

There is a module named getch which seems to allow reading character by character from stdin. Backspace is - quite strangely - mapped to an integer value of 127, but such a solution could then look like this:

def readLineWithAsterisks():
    sBuffer = ''
    while True:
        c = getch.getch()
        if c == '\n':
            return sBuffer
        elif ord(c) == 127:
            if len(sBuffer) > 0:
                sys.stdout.write('\x08 \x08')
                sys.stdout.flush()
                sBuffer = sBuffer[0:-1]
            continue
        else:
            sys.stdout.write('*')
            sys.stdout.flush()
            sBuffer += c

But this code has some drawbacks. First I'm very much confused about c not being '\b' if s.o. entered a backspace. Maybe s.o. has an explanation for this? Second only ASCII characters are processed, at least on Linux. I don't know about Windows here, but if a character other than A-Z0-9 is pressed, the line c = getch.getch() will throw an exception. getch() does not seem to be able to process umlauts and other kinds of characters, at least to some extent.

To come to solution input the following issues should be addressed:

  • How can a read from stdin be done character by character in regard to non-ASCII characters?
  • How can this be done in a platform independent way?
  • How can this been done safely (without security issues)? (getpass somehow seems to address this but I don't fully understand how.)
like image 404
Regis May Avatar asked Nov 17 '16 09:11

Regis May


People also ask

How can I make user input password hidden with asterisk in Python?

There are various Python modules that are used to hide the user's inputted password, among them one is maskpass() module. 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.

How do you input a password in Python?

The getpass() function is used to prompt to users using the string prompt and reads the input from the user as Password. The input read defaults to “Password: ” is returned to the caller as a string.

How do you output an asterisk in Python?

Print star or numberUse the print() function in each iteration of nested for loop to display the symbol or number of a pattern (like a star (asterisk * ) or number).


2 Answers

See the first answer there :

What's the simplest way of detecting keyboard input in python from the terminal?

Just print stars '*' or anything when a key is pressed.

All credit obviously goes to Phylliida for the research.

like image 172
Loïc Avatar answered Oct 16 '22 14:10

Loïc


You may want to look at how jupyter/ipython implemented this. I'm getting a dot displayed immediately for every character typed using getpass().

enter image description here

like image 1
denfromufa Avatar answered Oct 16 '22 16:10

denfromufa