September 14, 20223 min read

Create SHA256 Hash of a string in Python

Encryption and hashing have served as the foundation for new security modules, among other network security developments. One of the most used hash algorithms is the Secure Hash Algorithm(SHA) with a digest size of 256 bits, or SHA 256. Although there are numerous variations, SHA 256 has been the most often used in practical applications.

SHA 256 is a part of the SHA 2 family of algorithms, where SHA stands for Secure Hash Algorithm. It was a joint effort between the NSA and NIST to introduce a successor to the weaker SHA 1 family. SHA2 was published in 2001 and has been effective ever since.

The hash function generates the same output hash for the same input string. This means that, you can use this string to validate files or text or anything when you pass it across the network or even otherwise. SHA256 can act as a stamp or for checking if the data is valid or not.

The 256 in the name SHA256 refers to the final hash digest value, meaning that regardless of the amount of plaintext or cleartext, the hash value will always be 256 bits.

For example -

Input StringOutput Hash
computer science is amazing! I love it.a3f2b30d5d6ef9006dd09741aa90d595d8a90666f3fc3c3ae4bf1c1e9a8e3042

SHA256 hash of a String in Python

SHA256 hash can be created using the python's default module hashlib. There are many more hash functions defined in the hashlib library.

The process of creating an SHA256 hash in python is very simple. First import hashlib, then encode your string that you want to hash i.e., converts the string into the byte equivalent using encode(), then pass it through the hashlib.sha256() function. We print the hexdigest value of the hash m, which is the hexadecimal equivalent encoded string.

Working code example-

import hashlib

text = 'Hello!'

m = hashlib.sha256(text.encode('UTF-8'))

Output of the above code-


The value you see here 334d016f755cd6dc58c53a86e183882f8ec14f52fb05345887c8a5edd42c87b7 is the SHA256 hash of the string Hello!.

The functions used in the above code-

  • encode() : Converts the string into bytes to be acceptable by hash function.
  • hexdigest() : Returns the encoded data in hexadecimal format.

You can also update the value of the string and check it as well if needed. This can be used to strengthen the hash logic in your workflow where you can append strings in certain order and see if your hash matched the source hash.

import hashlib

text = 'Hello!'

m = hashlib.sha256()
m.update(b"Have Fun!")

Output of the above code-


As you see, the SHA256 hash of a string using Python is as simple as this code.

The above code just produced SHA256 hash of the string alone, but, to strengthen the security you can also generate SHA256 hash with salt as well.

In case you are looking to create SHA256 hash of a file or a blob check out this article.

A more complex hash can be created in SHA 2 family using the SHA-512 algorithm in python for a file.

I'm glad that you found the content useful. Happy Coding.

Share this blog
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
Next Article