Enable Dark Mode!
By: Cybrosys Technologies

Hashing Algorithms Using Python


Security is a primary concern for all applications. So in Odoo, the passwords are hashed so that they can never be used and modified by an unauthorized person. 
The process of converting a string/key to another value is known as hashing. Moreover, the value is generated using a specific mathematical function known as a hash function.
This blog will provide you an insight on hashing algorithms using python.
The hash function helps to store strings such as passwords in the form of hash values so that it becomes impossible for the unauthorized users from decrypting it. In addition, the intruder cannot log in to an application using this hash value.
The generated value is known as the hash. Usually, the length of the string which is converted to hash is smaller than the hash code. Moreover, the hash value is obtained with the help of a hash function, this process of obtaining hash code requires a series of steps. Therefore, the algorithm, which describes how to use the hash functions is known as a hashing algorithm.
Hashing is a unidirectional process which means it is not possible to get the original data from the hash. Additionally, the MD-5(Message-Digest Algorithm 5) and  SHA(Secure Hash Algorithm) family algorithms are the most commonly used hashing algorithms.
Now let's move on to understanding the Message digest algorithm -5 in the next section.

MD5 (Message-Digest Algorithm 5)
MD5 algorithm generates a hash value of 128-bit length. Moreover, the MD5 algorithm can be easily understood by the following example code which generates an MD5 hash for a string in python 3.
import hashlib
result = hashlib.md5(b"Test password").hexdigest()
Output for the code is shown below:

The above code generates a hash value ffe24c9204d65518429aa701f3a6bdc5 for string ‘Test password’. 
In order to use the hashing algorithm, we need to import hashlib. Here the function b” ” is used to convert the string to bytes. Since the hashing function only accepts bytes. Additionally, the hexdigest() function converts the encoded data to hexadecimal format. 
MD5 algorithms are faster compared to SHA but the security of this algorithm is poor compared to the other algorithm.  
Now let us see the SHA family algorithms one by one.

The SHA256 algorithm is the most commonly used hashing algorithm and is more secure than the MD5 algorithm.
The following example code generates a SHA256 algorithm:
import hashlib
test_str = "password"
result = hashlib.sha256(test_str.encode())

The SHA256 algorithm belongs to the SHA-2 family. Additionally, here encode() function is used to convert string to bytes that is acceptable by the hash function.
The following are the main reasons for using SHA 256 algorithm is that:
    - It is more secure than other hashing algorithms
    - The collision possibility is less because there are 2256 possibilities for hash value, so the chance of having the same hash value for two different strings is lower.   
Let's now move on to understanding the SHA384 algorithm in the next section.

The SHA384 algorithm generates a hash value of 384-bit length. The following example code which generates a SHA384 algorithm:
import hashlib
test_str = "password"
result = hashlib.sha384(test_str.encode())
Let's now move on to understanding the SHA224 algorithm in the next section.

The SHA224 algorithm generates a hash value of 224-bit length. It belongs to the SHA2 family. The following example code which generates an SHA224 algorithm:
import hashlib
test_str = "password"
result = hashlib.sha224(test_str.encode())
Let's now move on to understanding the SHA512 algorithm in the next section.

The SHA512 algorithm creates a hash value of length 512 bits. It belongs to the SHA2 family.The following example code which generates an SHA512 algorithm:
import hashlib
test_str = "password"
result = hashlib.sha512(test_str.encode())
Let's now move on to understanding the SHA-1 algorithm in the next section.

The SHA-1 algorithm creates a 160-bit hash value. The usage of MD5 started to get replaced after the introduction of SHA-1. The below python code generates the hash value for the given string “password”:
import hashlib
test_str = "password"
result = hashlib.sha1(test_str.encode())
Moreover, the SHA-1 hashing algorithm has vulnerabilities so it is considered to be less resistant to attacks.
In conclusion, the most recommended algorithm to be used is SHA-256 as it generates a hash of length 256 bits. Hashing can also be used to check the integrity of files while transferring digital files. One such algorithm is CRC32(Cyclic Redundancy Check). Moreover, this hashing technique is used to detect accidental file changes. In addition, it is not that commonly used as compared to other hashing techniques. So it can be concluded that a hashing algorithm is considered to be secure if it offers no chance of hash collision thereby providing security.

Odoo Training Package
Odoo Implementation Package

If you need any assistance in odoo, we are online, please chat with us.

cybrosys youtube


Leave a comment


Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635


Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom


Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.


Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message