DebugPointer
Published on

Create SHA256 Hash of a string in Python

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
hi8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4
debugpointerce7a00e4bf3e576bceb605c846923a634051ca695ff8a3270af998959e72d265
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.

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.

Related articles

Let me summarize creating a hash in python, so that it becomes easy for you to refer-

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