How to Get the SHA256 Sum of a String Using Python


If you need to get the SHA256 sum of a string using Python, then you can do the following.

Step 1

Firstly, we need to use the hashlib module:

import hashlib

Step 2

Now make sure that the input string is encoded correctly.

your_input = "this is your input string".encode('utf-8')

Step 3

Finally use the sha256 function of the hashlib module, and get the hexdigest() value from it:

hashlib.sha256(your_input).hexdigest()

Putting it all together

import hashlib

your_input = "this is your input string".encode('utf-8')
hashed = hashlib.sha256(your_input).hexdigest()

print(hashed)

# --> f7dc376447dcd13fdd6cb2aa20a2327bed1f8a1f3420d52c6fdb55b2b94d9b8e