Masking User Input in Python With Asterisks

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

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/

How to mask user password input and save it as output to replace default password in another file

Leaving other problems of your approach aside, I'll focus on your "Unwanted result: Masked input password didnt replace default password."

  1. You have a global list1, and you define a list1 in the getPASS() function as well, which you want to use after returning from the function. This doesn't work as is, since the list1 in the function is local to it and distinct from the global list1. I recommend to return a proper value from the function, e. g.:

    def getPASS():
    print("Input password")
    list1 = []
    while True:

    return "".join(list1)


    psw = getPASS()
  2. The logic of password input and verification got mixed up here:

    psw = "".join(list1)
    print(psw)
    invalid = ' ,:;/?"\}]{[-=+!@#$%^&*()|'
    for x in psw:
    if x in invalid:
    print("Character %r is not allowed in password" % x)
    getPASS()
    else:
    pass

    getPASS()

    You're trying to assign and verify the password before you even call getPASS() to input it, therefore psw is empty. Rearrange that, e. g. (assuming above 1. change to getPASS()):

    while True:
    psw = getPASS()
    print(psw) # only for debugging
    invalid = ' ,:;/?"\}]{[-=+!@#$%^&*()|'
    for x in psw:
    if x in invalid:
    print("Character %r is not allowed in password" % x)
    break # from for x in psw
    else:
    break # from while True

Masking keyboard input with a character - Python version 3.0

I think I have just what you need.
Password is set to variable name 'psw'.
This only seems to work in cmd though.

import msvcrt
import time
import sys
import os

def getPASS():
print("Input password")
list1 = []
while True:
char = msvcrt.getch()
char =str(char)
char = char[2:-1]
if char == "\\n'" or char == "\\r":
break
elif char == "\\x08":
del list1[-1]
os.system("cls")
print("Input password:")
sys.stdout.write("*" * len(list1))
sys.stdout.flush()
continue
else:
list1.append(char)
sys.stdout.write("*")
sys.stdout.flush()
print("\n")
psw = "".join(list1)
print(psw)
invalid = ' ,:;/?"\}]{[-=+!@#$%^&*()|'
for x in psw:
if x in invalid:
print("Character %r is not allowed in password" % x)
getPASS()
else:
pass

getPASS()

Please give any improvements, Dan



Related Topics



Leave a reply



Submit