Skip to main content

Insecure encryption algorithm - SHA1


Upgrade to a secure encryption algorithm


  • Usage of PHP for server-side scripting and web development
  • Usage of PHP for server-side scripting and web development


Non compliant code

// Hashing a password using SHA1
$password = "password";
$hashedPassword = sha1($password);
echo $hashedPassword;

The above code is a simple PHP script that uses the sha1 hashing algorithm to hash a password.

The sha1 function in PHP is a hashing algorithm that was once considered secure, but is now known to have multiple vulnerabilities. It produces a 160-bit (20-byte) hash value known as a message digest, typically rendered as a hexadecimal number, 40 digits long.

The issue with SHA1 is that it is no longer considered secure against well-funded attackers. It is computationally inexpensive to generate a SHA1 hash and there are known collision vulnerabilities, meaning that different inputs can produce the same hash output.

In the context of password storage, if an attacker is able to gain access to the hashed passwords, they could use a rainbow table (a precomputed table for reversing cryptographic hash functions) to easily and quickly find a password that produces the same hash, thus gaining unauthorized access.

In the context of this code, the use of the sha1 function to hash a password is the insecure encryption vulnerability.


  • Identify all instances where SHA1 is used for encryption in your code.
  • Replace SHA1 with a more secure encryption algorithm. A good choice would be SHA-256 or SHA-3.
  • Ensure that the new encryption algorithm is used consistently throughout your code.
  • Test your application to ensure that the new encryption algorithm works as expected.
  • Review your code regularly to ensure that insecure encryption algorithms are not reintroduced.

Compliant code

// Hashing a password using SHA-256
$password = "password";
$hashedPassword = hash('sha256', $password);
echo $hashedPassword;

The original code was using the SHA1 encryption algorithm, which is considered insecure due to its vulnerability to collision attacks. In the fixed code, we have replaced SHA1 with SHA-256, a more secure encryption algorithm.

The hash function in PHP is used to generate a hash value using the SHA-256 algorithm. The first parameter of the hash function is the algorithm to be used ('sha256' in this case), and the second parameter is the string to be hashed.

After the change, the $hashedPassword will now hold the SHA-256 hash of the password, which is significantly more secure than the SHA1 hash.

It's important to note that while SHA-256 is currently considered secure, encryption standards can change over time as new vulnerabilities are discovered and new encryption algorithms are developed. Therefore, it's important to regularly review and update your encryption methods as needed.

Also, remember to test your application thoroughly after making these changes to ensure everything works as expected.