Python hashlib Module:
To generate a message digest or secure hash from the source message, we can utilize the Python hashlib library.
The hashlib module is required to generate a secure hash message in Python.
The hashlib hashing function in Python takes a variable length of bytes and converts it to a fixed-length sequence. This function only works in one direction. This means that when you hash a message, you obtain a fixed-length sequence. However, those fixed-length sequences do not allow you to obtain the original message.
A hash algorithm is considered better in cryptography if the original message cannot be decoded from the hash message. Changing one byte in the original message also has a big impact(change) on the message digest value.
Python secure hash values are used to store encrypted passwords. So that even the application’s owner does not have access to the user’s password, passwords are matched when the user enters the password again, and the hash value is calculated and compared to the stored value.
Hashing Algorithms That Are Available:
- The algorithms_available function returns a list of all the algorithms available in the system, including those accessible via OpenSSl. Duplicate algorithm names can also be found.
- The algorithms in the module can be viewed by using the algorithms_guaranteed function.
import hashlib # Printing list of all the algorithms print(hashlib.algorithms_available) # Viewing algorithms print(hashlib.algorithms_guaranteed)
Output:
{'sha384', 'blake2s', 'sha3_384', 'sha224', 'md5', 'shake_256', 'blake2b', 'sha3_512', 'sha1', 'shake_128', 'sha512', 'sha3_256', 'sha256', 'sha3_224'} {'sha384', 'blake2s', 'sha3_384', 'sha224', 'md5', 'shake_256', 'blake2b', 'sha3_512', 'sha1', 'shake_128', 'sha512', 'sha3_256', 'sha256', 'sha3_224'}
Functions:
You only need to know a few functions to use the Python hashlib module.
- You can hash the entire message at once by using the hashlib.encryption_algorithm_name(b”message”) function.
- Additionally, the update() function can be used to append a byte message to the secure hash value. The output will be the same in both cases. Finally, the secure hash can be obtained by using the digest() function.
- It’s worth noting that b is written to the left of the message to be hashed. This b indicates that the string is a byte string.
hashlib.sha3_384() Function:
We can convert a normal string in byte format to an encrypted form using the hashlib.sha3_384() function. Passwords and important files can be hashed to secure them using the hashlib.sha3_384() method.
Syntax:
hashlib.sha3_384()
Return Value:
The hash code for the string given is returned by the sha3_384() function.
Differences
Shortly after the discovery of cost-effective brute force operations against SHA-1, SHA-2 was created. It is a family of two similar hash algorithms, SHA-256 and SHA-512, with varying block sizes.
- The fundamental distinction between SHA-256 and SHA-512 is word size.
- SHA-256 uses 32-byte words, whereas SHA-512 employs 64-byte words.
- Each standard also has modified versions called SHA-224, SHA-384, SHA-512/224, and SHA-512/256. Today, the most often used SHA function is SHA-256, which provides adequate safety at current computer processing capabilities.
- SHA-384 is a cryptographic hash that belongs to the SHA-2 family. It generates a 384-bit digest of a message.
- On 64-bit processors, SHA-384 is around 50% faster than SHA-224 and SHA-256, despite having a longer digest. The increased speed is due to the internal computation using 64-bit words, whereas the other two hash algorithms use 32-bit words.
- For the same reason, SHA-512, SHA-512/224, and SHA-512/256 are faster on 64-bit processors.
Algorithm – digest size (the larger the better):
MD5 –> 128 bits
SHA-1 –> 160 bits
SHA-256 –> 256 bits
SHA-512 –> 512 bits
hashlib.sha3_384() Function in Python
Method #1: Using sha3_384() Function (Static Input)
Here, we encrypt the byte string or passwords to secure them using the hashlib.sha3_384() function.
Approach:
- Import hashlib module using the import keyword
- Create a reference/Instance variable(Object) for the hashlib module and call sha3_384() function and store it in a variable
- Give the string as static input(here b represents byte string) and store it in another variable.
- Call the update() function using the above-created object by passing the above-given string as an argument to it
- Here it converts the given string in byte format to an encrypted form.
- Get the secure hash using the digest() function.
- The Exit of the Program.
Below is the implementation:
# Import hashlib module using the import keyword import hashlib # Creating a reference/Instance variable(Object) for the hashlib module and # call sha3_384() function and store it in a variable obj = hashlib.sha3_384() # Give the string as static input(here b represents byte string) and store it in another variable. gvn_str = b'Python-programs' # Call the update() function using the above created object by passing the above given string as # an argument to it # Here it converts the given string in byte format to an encrypted form. obj.update(gvn_str) # Get the secure hash using the digest() function. print(obj.digest())
Output:
b'\xf5;.\x1f\xb3$\xfd\xc2s7\xa5\x13\xa9\xc9\xceB\xd1A,u\x7fy\xeaw\xab\x07\x0b,lN\xefg\x86\x04t\xb0~\xd9\xe4_\xed\xc2\x07\xdb\xad$\xf4\xea'
Method #2: Using sha3_384() Function (User Input)
Approach:
- Import hashlib module using the import keyword
- Create a reference/Instance variable(Object) for the hashlib module and call sha3_384() function and store it in a variable
- Give the string as user input using the input() function and store it in another variable.
- Convert the given string into a byte string using the bytes() function by passing the given string, ‘utf-8’ as arguments to it.
- Call the update() function using the above-created object by passing the above-given string as an argument to it
- Here it converts the given string in byte format to an encrypted form.
- Get the secure hash using the digest() function.
- The Exit of the Program.
Below is the implementation:
# Import hashlib module using the import keyword import hashlib # Creating a reference/Instance variable(Object) for the hashlib module and # call sha3_384() function and store it in a variable obj = hashlib.sha3_384() # Give the string as user input using the input() function and store it in another variable. gvn_str = input("Enter some random string = ") # Convert the given string into byte string using the bytes() function by passing given string, # 'utf-8' as arguments to it gvn_str=bytes(gvn_str, 'utf-8') # Call the update() function using the above created object by passing the above given string as # an argument to it # Here it converts the given string in byte format to an encrypted form. obj.update(gvn_str) # Get the secure hash using the digest() function. print(obj.digest())
Output:
Enter some random string = welcome to Python-programs b'\xe4\xf2\xe03\xcbG\xe5YH.\xe2\x13W\xbc\xd0pQ\xd4dYshc=\xdc\xeeY\xf9\xa5dX\xda\x8c\x08\x18\xa1\xe4,\xe6z\x99x\xf2\xa4l\xe4\xc4$'