- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SHA in Python
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.
- Related Articles
- SHA encoding using Python?
- What is SHA in Information Security?
- What is the difference between SHA and MD5 in Information Security?
- Generating a SHA-256 hash from the Linux command line
- abs() in Python
- Generators in Python?
- Counters in Python?
- classmethod() in Python
- Comprehensions in Python
- Datagram in Python
- Enumerate() in Python
- Calendar in python
- chr () in Python
- exec() in Python
- factorial() in Python

Advertisements