Python convert user input to asterisk [SOLVED]


Written by - Azka Iftikhar
Reviewed by - Deepak Prasad

Different methods to convert user input to asterisk in Python

In this tutorial we will cover different methods which we can use in Python to convert user input to asterisk. This is required when we are prompting user to input password and we don't want anyone to read the password entered by user. It is easier to just hide the user input but it would look mush better and professional to print asterisk for every key entered by user in the input.

We will explore 3 methods to achieve this scenario:

  • getpass
  • pwinput
  • stdiomask

Python getpass is a method for securing password details. In any system it is important to keep the password details hidden, otherwise the system can be hacked easily, which is not something a client would want. In java or c/c++ we need to implement the masking techniques ourselves but python has the python getpass and other libraries that help in the masking of passwords.

Let us look at the different techniques of masking a password. We will be using Python 3.9 for testing all the programs in this article.

 

Method-1: Getpass

The python getpass library helps mask password. Although it doesn't cover our basic agenda of adding asterisk to password but we will use getpass with other modules in next examples. The implementation of code is pretty simple.

import getpass

# Hide the password
password = getpass.getpass()
print(password)

Make sure you have the getpass package installed. If not, type the following command in your terminal

$ pip install getpass

 

Method-2: pwinput

Another package that works as python getpass is pwinput, this package masks the password into any character that you want in our case lets take asterisk as an example

Install pwinput package

$ pip install pwinput

The output must be as follows :

Collecting pwinput
  Using cached pwinput-1.0.2-py3-none-any.whl
Installing collected packages: pwinput
Successfully installed pwinput-1.0.2

Next import pwinput to your code. Pass the prompt message as an argument to the pwinput function and the character you want to mask it with as another argument.

The code is as follows :

import pwinput

# python convert user input to asterisk
password = pwinput.pwinput(prompt ="Enter your password: ", mask="*")
print("Your password is:", password)

The output of the code is as follows

Enter your password: ****
Your password is: 1234

 

Method-3: stdiomask

Stdio mask is another package that works as python getpass asterisk , you need to install the package first by using the command below

$ pip install stdio

The output should be as follows

Collecting stdiomask
  Using cached stdiomask-0.0.6-py3-none-any.whl
Installing collected packages: stdiomask
Successfully installed stdiomask-0.0.6

After installing stdiomask on your system, import it to your program import stdiomask. Use python getpass function password = stdiomask.getpass()


If you want you can also prompt the user with a message like “Enter your password” or “input password” etc. this can be done using the prompt=”your message” argument to the python getpass method. The code for this is as follows

import stdiomask

# Convert user input to asterisk
password = stdiomask.getpass(prompt="Enter password : ")
print("Your password is:", password)

Output of this code is :

Enter password : ****
Your password is: 1234

Example : A complete login page and security questions

This program is an example of how we can use getpass and other if and while conditions are included to make it more secure.

import stdiomask
import getpass
print ("welcome to the secure system")
passw=stdiomask.getpass(prompt="Enter password: ")
while(1):
    if(passw=="1234"):
        print("Password Correct\n Answer security question ")
        securityQuestion= stdiomask.getpass(prompt="what is your birth date? ")
        if(securityQuestion=="19/03/2002"):
            print("YOU ARE IN...")
        else:
            print("sorry try again...")
    else:
        print("incorrect passoword...")
    break

The output of this code is

welcome to the secure system
Enter password: ****
Password Correct
 Answer security question
what is your birth date? **********
YOU ARE IN...

 

In case of wrong credentials

~]# python3 script.py 
welcome to the secure system
Enter password: *****
incorrect passoword...

 

Conclusion

We studied different masking ways in this article. Python getpass is a function that allows you to mask the password of a user. It can be used in different packages that we learned in this article, including getpass, stdiomask and pwinput. Since getpass directly does not support an asterisk masking, we use other packages for that. In the end we looked at an example of how we can use getpass as a secure password input method. This can help in a lot of information security building systems.

 

Further Reading

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

 

Views: 11

Azka Iftikhar

She is proficient in multiple programming languages, including C++, Golang, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can read more on her GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment