Python Program to Calculate SHA-256, SHA-512 and MD5 Hashes

Published November 22, 2024

How to find SHA-256, SHA-512 and MD5 hashes in Python How to find SHA-256, SHA-512 and MD5 hashes in Python

In Python, you can use the hashlib module to find the MD5 hash, SHA-256 hash and SHA-512 hash of a string or binary file.

Using the hashlib module

If your Python program is going to calculate the MD5, SHA-256 or SHA-512 hashes of a string, first, import the hashlib module.

import hashlib

Find all supported hash algorithms

SHA-256, SHA-512 and MD5 are not the only secure cryptographic hashes and message digest algorithms.

To find all the supporting hash algorithms, run this:

import hashlib
print(hashlib.algorithms_guaranteed)

Output:

{'shake_256', 'shake_128', 'sha3_384', 'sha1', 'sha256', 'md5', 'sha384', 'sha224', 'sha3_256', 'sha3_224', 'blake2s', 'sha3_512', 'blake2b', 'sha512'}

Find the MD5 value of a string

To find the MD5 value of this string This is a sample sentence., we can write a program like this:

import hashlib

sentence = 'This is a sample sentence.'
hashed = hashlib.md5(sentence.encode('utf-8')).hexdigest()
print(hashed)
  1. The first step is to encode the string sentence as UTF-8 or something similar.
  2. Next, call the hashlib.md5() method and pass the encoded string as parameter.
  3. Finally, call the .hexdigest() method over the output from (2). That will give you the MD5 hash of the string.

Output:

29c85161bd86afd6dda7d06220e8a9d9

Find the SHA-256 hash value of a string

To find the SHA-256 hash value of this string This is a sample sentence., our program can be like this:

import hashlib

sentence = 'This is a sample sentence.'
hashed = hashlib.sha256(sentence.encode('utf-8')).hexdigest()
print(hashed)
  1. The first step is to encode the string sentence as UTF-8 or something similar.
  2. Next, call the hashlib.sha256() method and pass the encoded string as parameter.
  3. Finally, call the .hexdigest() method over the output from (2). That will give you the MD5 hash of the string.

Output:

6ed219ce8ad31d77efd6b917c2a18d7336111a3c2d987c67523bba9e211c5981

Find the SHA-512 hash value of a string

To find the SHA-512 hash value of this string This is a sample sentence., our program can be like this:

import hashlib

sentence = 'This is a sample sentence.'
hashed = hashlib.sha512(sentence.encode('utf-8')).hexdigest()
print(hashed)

Output:

afc3f36159c6abad294715b1e12bf2b23f57ac2aedd03eab18afcda2b3ac80e43625381cc8c493d209390e5d86362d4ec8c0169cde644ab5ffff6248686a106a

Other hash algorithms

If you want to calculate the hash value of other hash functions like SHA-1, just look at the output of this attribute

import hashlib
print(hashlib.algorithms_guaranteed)

Conclusion

If you found this blog post useful, feel free to share it. Thanks for reading.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: November 22, 2024.     This post was originally written on November 17, 2024.