Create
python
September 24, 20224 min read

Create SHA3-512 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 512 bits, or SHA 512. Although there are numerous variations, SHA 512 has been the most often used in practical applications.

SHA-3 (Secure Hash Algorithm 3) is the latest member of the Secure Hash Algorithm family of standards. Although part of the same series of standards, SHA-3 is internally different from the MD5-like structure of SHA-1 and SHA-2. SHA-3 instances are drop-in replacements for SHA-2, intended to have identical security properties. The SHA-3 family consists of six hash functions with digests (hash values) that are 128, 224, 256, 384 or 512 bits: SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128, SHAKE256.

The SHA-3 or Keccak algorithm is one of the most secure and efficient hashing algorithms and some claim that it won’t be cracked in the next 20 - 30 years. Developments in the quantum computing world might decrease that time frame but it is still one of the best hashing algorithm we have got right now.

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. SHA3-512 can act as a stamp or for checking if the data is valid or not.

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

For example -

Input StringOutput Hash
hi154013cb8140c753f0ac358da6110fe237481b26c75c3ddc1b59eaf9dd7b46a0a3aeb2cef164b3c82d65b38a4e26ea9930b7b2cb3c01da4ba331c95e62ccb9c3
debugpointerac09d8e98cd7d60927600334167ca22a79fad316a8af4fdc1d076c6cfe72c44778cc47223eb296f3ddc13dcfd4bb395f6346c0f29f2f2f4a46f4d9bcee63d1df
computer science is amazing! I love it.308a9fd755db73031ddae5f58734c8b57db3bc5ea24bfc6de7df962158e24bbe697219a72ccdeaa03c1848f2c35ef46d050ec0b678465de309e8a43bdf248c64

SHA3-512 hash of a String in Python

SHA3-512 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 SHA3-512 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.sha3_512() 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.sha3_512(text.encode('UTF-8'))
print(m.hexdigest())

Output of the above code-

5156b8639729070b538a4d57fe78004876376a6503adfe8f48c57da7a959f74162b85f5341799dd9f91657dfc81b75035ddbaed193e7d5f6fbbb62d07c4a4240

The value you see here 5156b8639729070b538a4d57fe78004876376a6503adfe8f48c57da7a959f74162b85f5341799dd9f91657dfc81b75035ddbaed193e7d5f6fbbb62d07c4a4240 is the SHA3-512 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.sha3_512()
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-

40d24aae5c03a78ae5ad777ea1e4de2930782a1aa414d1bc96b894953d2dae1668363139cbce3d480380a25456be7df51107e2cb13583b4a260a5c5d1c6517e0

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

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

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

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
Satvik
Entrepreneur
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