Create SHA3-256 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-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-256 can act as a stamp or for checking if the data is valid or not.
The 256 in the name SHA3-256 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|
|computer science is amazing! I love it.||9d9c88852fed897f23a898f0994b325cff9c70b629c96eff3739c4ffb1459edf|
SHA3-256 hash of a String in Python
SHA3-256 hash can be created using the python's default module
hashlib. There are many more hash functions defined in the
The process of creating an SHA3-256 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_256() function. We print the
hexdigest value of the hash
m, which is the hexadecimal equivalent encoded string.
Working code example-
import hashlib text = 'Hello!' hash_sha3_256 = hashlib.sha3_256(text.encode('UTF-8')) print(hash_sha3_256.hexdigest())
Output of the above code-
The value you see here
334d016f755cd6dc58c53a86e183882f8ec14f52fb05345887c8a5edd42c87b7 is the SHA3-256 hash of the string
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!' hash_sha3_256 = hashlib.sha3_256() print(hash_sha3_256.hexdigest()) hash_sha3_256.update(b"Have Fun!") print(hash_sha3_256.hexdigest()) hash_sha3_256.update(text.encode('UTF-8')) print(hash_sha3_256.hexdigest())
Output of the above code-
As you see, the SHA3-256 hash of a string using Python is as simple as this code.
The above code just produced SHA3-256 hash of the string alone, but, to strengthen the security you can also generate SHA3-256 hash with salt as well.
In case you are looking to create SHA3-256 hash of a file or a blob check out this article.
A more complex hash can be created in SHA 3 family using the SHA3-512 algorithm in python for a file.
I'm glad that you found the content useful. Happy Coding.