As a Python enthusiast, I’m always on the lookout for ways to enhance my coding skills and ensure the security of my data. One powerful technique I often turn to is creating SHA256 hashes of strings. SHA256 is a widely-used cryptographic hash function known for its strong security properties. In this blog post, I’ll guide you through the process of creating SHA256 hashes of strings in Python. By the end of this tutorial, you’ll have a valuable tool at your disposal to protect sensitive data and verify its integrity.
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. There are weaker predecessors to SHA3 like MD5, SHA1, SHA2. Interested in knowing the difference between SHA1, SHA2 & SHA3, this will give you a great insight on how SHA has evolved over the years.
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 String | Output Hash |
---|---|
hi | 8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4 |
debugpointer | ce7a00e4bf3e576bceb605c846923a634051ca695ff8a3270af998959e72d265 |
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'))
print(m.hexdigest())
Output of the above code-
334d016f755cd6dc58c53a86e183882f8ec14f52fb05345887c8a5edd42c87b7
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()
print(m.hexdigest())
m.update(b"Have Fun!")
print(m.hexdigest())
m.update(text.encode('UTF-8'))
print(m.hexdigest())
Output of the above code-
056ef8e9b0c5fe400c17a1f68cab224498a914c649009fed48ff9aa8e6daeb8c
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 the article.
You can create a more complex hash in SHA 2 family using the SHA-512 algorithm in python for a file.
I’m glad that you found the content useful. We’ve reached the end of our journey through the creation of SHA256 hashes of strings in Python. I hope you found this tutorial both informative and empowering. With the ability to generate SHA256 hashes, you now possess a powerful tool for securing sensitive data and ensuring its authenticity. Remember, the strength of a cryptographic hash function lies not only in its security but also in its versatility. Feel free to explore further and apply your newfound knowledge to various applications and use cases. Happy hashing! Happy Coding.