Python hashlib – SHA1 hash generator

In this post we will be creating a Python script that will generate SHA1 hash strings using the hashlib library. The hashlib library will allow us to easily generate secure cryptographic hash functions. People commonly use these hash functions to store passwords, verify data integrity, and perform other cryptographic applications. The library offers a wide range of available hash functions which make it a valuable tool for any developer seeking to implement strong security measures.

In this example we will be generating an SHA1 hash which stands for Secure Hash Algorithm 1. The hash function that generates a 160-bit hash value for a given input message. Interestingly the SHA1 hash function was developed by the United States National Security Agency (NSA)

See the snippet of Python code where we use the hashlib library to generate a SHA1 hash string. The script utilizes a while loop where the user can enter a string to hash and the hash will be returned to output.

import hashlib

while True:
    string = input("Enter string to hash: ")
    sha1_hash = hashlib.sha1(string.encode()).hexdigest()
    print("SHA1 hash is", sha1_hash)

See some example output below.

Enter string to hash: password123
SHA1 hash is cbfdac6008f9cab4083784cbd1874f76618d2a97
Enter string to hash: facebook007
SHA1 hash is e40862079c1449ba4b7724c466de6cbff59ac83c

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

Leave a Reply