In this tutorial, we are going to learn about the hashlib module that gives us different SHA. (Secure Hash Algorithms) is set of cryptographic hash functions.
Let's install the module by typing the following command.
pip install hashlib
We can see the available algorithms in the hashlib module using algorithms_guaranteed set. Let's see them by running the following code.
Example
# importing the hashlib module import hashlib # printing available algorithms print(hashlib.algorithms_guaranteed)
Output
If you run the above code, then you will get the following result.
{'sha256', 'sha512', 'sha224', 'shake_256', 'blake2s', 'shake_128', 'sha384', 'sha3_384', 'sha3_512', 'sha3_224', 'md5', 'sha3_256', 'sha1', 'blake2b'}
Example
Let's see an example on how to user sha256 algorithm.
# importing the hashlib module import hashlib # initialinzing a string # the string will be hashed using the 'sha256' name = 'Tutorialspoint' # convert the string to bytes using 'encode' # hash functions only accepts encoded strings encoded_name = name.encode() # Now, pass the encoded_name to the **sha256** function hashed_name = hashlib.sha256(encoded_name) # we have hashed object # we can't understand it # print the hexadecimal version using 'hexdigest()' method print("Object:", hashed_name) print("Hexadecimal format:", hashed_name.hexdigest())
Output
If you run the above code, then you will get the following result.
Object: <sha256 HASH object @ 0x000002A416E1BAE0> Hexadecimal format: 447c2329228a452aa77102dc7d4eca0ee4c6d52a17e9c17408f8917e51e 3
Conclusion
You can use the remaining algorithms similar to the sha256. If you have any queries in the tutorial, mention them in the comment section.