Python getpass – Hash password

In this post we will be creating a Python script that will hash a given password, to help us achieve this we will be using the ‘getpass’ module. The “getpass” module in Python provides a secure way to collect sensitive information, such as passwords, from the user.

See the snippet of Python code below where we utilize the ‘getpass’ module to securely hash a password. Below we make use of the ‘getpass()’ function to conceal what is typed. Using the ‘prompt’ argument we are able to set the text displayed to the user. We then use the ‘hashlib’ module to hash the password using the SHA-256 hash function. The hashed password is then returned to output.

import getpass
import hashlib
import time

password = getpass.getpass(prompt="Enter the password: ")

password_hash = hashlib.sha256(password.encode()).hexdigest()

print("Password hash:", password_hash)

time.sleep(10)

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply